0

这里将数据发布到 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();
    }

这里添加logcat

   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}}
4

2 回答 2

0

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

从 Android 3.0 及更高版本开始,您不再被允许从 UI 线程执行网络操作,否则NetworkOnMainThreadException会抛出 a。

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

于 2013-09-11T07:21:21.980 回答
0

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

对所有 http 请求使用AsyncTask

于 2013-09-11T07:22:50.800 回答