Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.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
例外:android中的noHttpResponse_Android - Fatal编程技术网

例外:android中的noHttpResponse

例外:android中的noHttpResponse,android,Android,这里将数据发布到url并从中获取响应。但现在获取noHttpResponse异常 这里放置代码 TextView xx = (TextView) findViewById(R.id.xx); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost( "http://motinfo.direct.gov.uk/internet/jsp/ECHID-Internet-H

这里将数据发布到url并从中获取响应。但现在获取noHttpResponse异常 这里放置代码

TextView xx = (TextView) findViewById(R.id.xx);
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(
            "http://motinfo.direct.gov.uk/internet/jsp/ECHID-Internet-History-Request.jsp");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs
                .add(new BasicNameValuePair(
                        "Vehicle registration mark from number plate",
                        "123456789"));
        nameValuePairs.add(new BasicNameValuePair("MOT test number",
                "AP3398"));
        nameValuePairs.add(new BasicNameValuePair("MOT test number",
                "000000"));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = client.execute(post);

        String line = "";
        if (response != null) {
            System.out
                    .println("***********************************************************");
            xx.setText(EntityUtils.toString(response.getEntity()));

        }

    } catch (IOException e) {
        e.printStackTrace();
    }

您应该将执行http请求的代码片段提取到方法中,并在单独的线程中执行,例如使用
AsyncTask
IntentService

从Android 3.0及更高版本开始,不再允许您从UI线程执行网络操作,否则将引发
NetworkOnMainThreadException


对于Android 2.x,您不会得到该异常,但http操作可能需要太长时间才能执行并阻止UI线程,这可能会产生ANR(应用程序无响应)错误,这似乎是您的情况。

根据您的日志,我可以看到UI线程正在阻塞,因为您正在发送POST请求,该请求阻塞了等待响应的应用程序


用于所有http请求

您面临的是
ANR
状态,因此可能会出现异常,所有这些可能是由于internet连接速度慢或从响应中获取的
JSON
数据量大。您是否在设备上进行了测试?然后您将得到应用程序未响应错误。要解决这个问题,您应该从线程发出http请求。
   09-11 12:40:22.086: I/InputDispatcher(61): Application is not responding: AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}.  640698.7ms since event, 640698.2ms since wait started
09-11 12:40:22.086: I/WindowManager(61): Input event dispatching timed out sending to application AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}
09-11 12:40:27.117: I/InputDispatcher(61): Application is not responding: AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}.  645729.5ms since event, 645728.9ms since wait started
09-11 12:40:27.117: I/WindowManager(61): Input event dispatching timed out sending to application AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}
09-11 12:40:32.165: I/InputDispatcher(61): Application is not responding: AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}.  650777.7ms since event, 650777.1ms since wait started
09-11 12:40:32.165: I/WindowManager(61): Input event dispatching timed out sending to application AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}
09-11 12:40:37.182: I/InputDispatcher(61): Application is not responding: AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}.  655793.8ms since event, 655793.3ms since wait started
09-11 12:40:37.183: I/WindowManager(61): Input event dispatching timed out sending to application AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}
09-11 12:40:42.202: I/InputDispatcher(61): Application is not responding: AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}.  660814.3ms since event, 660813.7ms since wait started
09-11 12:40:42.202: I/WindowManager(61): Input event dispatching timed out sending to application AppWindowToken{406a9208 token=HistoryRecord{4064bf48 com.example.xxx/.MainActivity}}