0

When i implement this it says "NetworkOnMainThreadException".

Could someone post an example which works?

I have tried many different examples and libraries, but it didnt work. my code:

private class HttpGetter extends AsyncTask<URL, Void, Void> {

    @Override
    protected Void doInBackground(URL... urls) {
        // TODO Auto-generated method stub
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(String.valueOf(urls[0]));

        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
                Log.v("Getter", "Your data: " + builder.toString()); //response data
            } else {
                Log.e("Getter", "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


}
3
  • are you sure this is your only code where you are executing http tasks? could you show the logcat? Commented Oct 20, 2014 at 14:47
  • Show how you are calling it and explain how it isn't working. Are you still getting NetworkOnMainThreadException or something else is happening? Also, is this all of the task code? Do you override any of the other methods of AsyncTask? Commented Oct 20, 2014 at 14:48
  • You should be executing networking where you shouldn't in another place Commented Oct 20, 2014 at 14:50

2 Answers 2

1

how are you calling this class object. if you calling like this then you should not get that exception.

new HttpGetter().excute(new URL("http://www.urlstring.com"));

Alternatively you can run your code with below code without exception but NOT RECOMMENDED

StrictMode.ThreadPolicy policy = new      StrictMode.ThreadPolicy.Builder().permitAll().build();

 StrictMode.setThreadPolicy(policy); 

ADD this permission in android manifest.xml file:

<uses-permission android:name="android.permission.INTERNET"/>
Sign up to request clarification or add additional context in comments.

Comments

0

You should execute the task like this new HttpGetter().execute(URL)

Note that AsyncTask can be executed only once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.