Android执行HTTPGet不在此行

时间:2013-06-26 16:27:34

标签: android http-get

try {
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours");
    HttpResponse response;

    response = client.execute(get);
    Toast.makeText(MainActivity.this, "WAD", Toast.LENGTH_SHORT).show();
    HttpEntity entity = response.getEntity();

    InputStream in = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line+"\n");
    }
    in.close();
    charset = sb.toString();
    tv.setText(charset);
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

在这一行中,

  

response = client.execute(get);

该程序似乎没有继续代码,它只是挂在那里,什么都不做。

我已申报互联网权限,但我找不到它的错误?

1 个答案:

答案 0 :(得分:2)

虽然您没有提及任何相关内容,但您的问题并不清楚,您可能正在主应用程序主题中执行网络操作。

如果您在Android 3.0或更高版本上进行测试,这将导致NetworkOnMainThreadException,但会在早期版本中使用户界面“挂起”/“冻结”。为避免这种情况,请始终在非UI线程上执行所有网络操作,例如AsyncTask