我正在尝试将一些网站的代码下载到我的应用程序中,如下所示:
public void wypned(final View pwn) throws IllegalStateException, IOException{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://example.com");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent() ) );
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
}
}
我得到的只是致命错误。LogCat 说:
Caused by: android.os.NetworkOnMainThreadException
on Android 3.x and up, you can't do network I/O on the main thread
有人可以告诉我如何解决吗?我试图用线程做一些事情,但没有成功。