I’m trying to download some website’s code to my app like this:
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";
}
}
And all i got is fatal error. LogCat says:
Caused by: android.os.NetworkOnMainThreadException
on Android 3.x and up, you can't do network I/O on the main thread
Could someone tell me how to solve it? I’ve tried to do something with threads but it didn’t work out.
Implement an asyncTask for doing that :
Call it from the activity: