Pawelnr1
Pawelnr1

Reputation: 233

Downloading html source on Android Crashes the Application

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.

Upvotes: 1

Views: 116

Answers (2)

Alexis C.
Alexis C.

Reputation: 93842

Implement an asyncTask for doing that :

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{

                        private Activity activity;
                        private ProgressDialog progressDialog;

            public MyAsyncTask(Activity activity) {
                            super();
                this.activity = activity;
            }

            @Override
            protected void onPreExecute() {
            super.onPreExecute();
                progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
            }

            @Override
            protected Result doInBackground(Void... v) {
            //do your stuff here
            return null;

            }

            @Override
            protected void onPostExecute(Result result) {
                progressDialog.dismiss();
                Toast.makeText(activity.getApplicationContext(), "Finished.", 
                        Toast.LENGTH_LONG).show();
            }


}

Call it from the activity:

MyAsyncTask task = new AsyncTask(myActivity.this);
task.execute();

Upvotes: 2

nullpotent
nullpotent

Reputation: 9260

All networking operations (as they are blocking) should be done on a separate thread. Android dev guide suggests this.

android.os.NetworkOnMainThreadException

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

Create a separate thread for your networking operations, it's telling you that basically.

Potentially long running operations such as network or database operations, or computationally expensive calculations such as resizing bitmaps should be done in a child thread

Source1 Source2

Upvotes: 0

Related Questions