Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java response=httpClient.execute(请求)不断给出NetworkOnMainThreadException_Java_Php_Android_Apache Httpclient 4.x_Jsonresponse - Fatal编程技术网

Java response=httpClient.execute(请求)不断给出NetworkOnMainThreadException

Java response=httpClient.execute(请求)不断给出NetworkOnMainThreadException,java,php,android,apache-httpclient-4.x,jsonresponse,Java,Php,Android,Apache Httpclient 4.x,Jsonresponse,我已经尝试了几个小时向php发送POST请求,php将返回json响应。但这一行代码一直给我带来问题,我似乎无法前进。我很沮丧。我查阅了无数其他人使用同一行的例子!但由于某种原因,它对我不起作用! 这是我的doinBackgrounds代码片段: protected Void doInBackground(Void... voids) { HttpClient httpClient=new DefaultHttpClient(); HttpGet request=new Http

我已经尝试了几个小时向php发送POST请求,php将返回json响应。但这一行代码一直给我带来问题,我似乎无法前进。我很沮丧。我查阅了无数其他人使用同一行的例子!但由于某种原因,它对我不起作用! 这是我的doinBackgrounds代码片段:

protected Void doInBackground(Void... voids) {
    HttpClient httpClient=new DefaultHttpClient();
    HttpGet request=new HttpGet();
    BufferedReader in=null;
    String data=null;
    HttpResponse response = null;
    try {
        URI website=new URI("login.php");
        request.setURI(website);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    HttpPost httpPost=new HttpPost("login.php");
    List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("name", name));
    nameValuePairs.add(new BasicNameValuePair("password", pwd));
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    try {
        response=httpClient.execute(request);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        String line=in.readLine();
        System.out.println(line);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (Utility.isNotNull(name) && Utility.isNotNull(pwd)) {
        RequestParams params = new RequestParams();
        if (Utility.validate(name, pwd)) {
            params.put("username", name);
            params.put("password", pwd);
            bloggedIn=true;
            onPostExecute();
        } else {
            loginActivity.InvalidToast();
        }
    } else {
        loginActivity.EmptyToast();
    }
    return null;
}
受保护的Void doInBackground(Void…voids){
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet请求=新建HttpGet();
BufferedReader in=null;
字符串数据=null;
HttpResponse响应=null;
试一试{
URI网站=新的URI(“login.php”);
setURI(网站);
}捕获(URISyntaxException e){
e、 printStackTrace();
}
HttpPost-HttpPost=newhttppost(“login.php”);
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“name”,name));
添加(新的BasicNameValuePair(“密码”,pwd));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
试一试{
response=httpClient.execute(请求);
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
in=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
String line=in.readLine();
系统输出打印项次(行);
}捕获(IOE异常){
e、 printStackTrace();
}
if(Utility.isNotNull(名称)和&Utility.isNotNull(pwd)){
RequestParams params=新的RequestParams();
if(实用程序验证(名称,pwd)){
参数put(“用户名”,名称);
参数put(“密码”,pwd);
bloggedIn=true;
onPostExecute();
}否则{
loginActivity.InvalidToast();
}
}否则{
loginActivity.EmptyToast();
}
返回null;
}

您正在自己调用
doInBackground()。这不是使用
AsyncTask
的方式。要执行
AsyncTask
,请创建一个实例并对其调用
execute()
executeOnExecutor()
。这将导致
doInBackground()
在后台线程上运行

您可以在中阅读有关
AsyncTask
的更多信息


(顺便说一句,将来,请将堆栈跟踪作为从LogCat复制的文本而不是屏幕截图发布)

请发布您的
NetworkOnMainThreadException
@commonware的整个堆栈跟踪我已经编辑了问题谢谢,它对我很有效