Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:56:07+00:00 2026-06-15T08:56:07+00:00

I’m using AsyncTask to send POST request to webservice, but I keep getting NetworkOnMainThreadException

  • 0

I’m using AsyncTask to send POST request to webservice, but I keep getting NetworkOnMainThreadException exception. I’m using solution from answer from this question :
How to fix android.os.NetworkOnMainThreadException?
but I can’t make it working.
My classes:

public class MainActivity extends Activity {

    public JSONObject result;
    public String retSrc;
    public HttpEntity resEntity;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        try {
            resEntity = new UpdateData().execute().get();     
            retSrc = EntityUtils.toString(resEntity);         // Here I get an exception
            result = new JSONObject(retSrc);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private class UpdateData extends AsyncTask<String, Void, HttpEntity>{

        private HttpEntity entit;
            @Override
            protected HttpEntity doInBackground(String... params) {
                try {
                    HttpClient client = new DefaultHttpClient();  
                    String postURL = "http://www.test.com";
                    HttpPost post = new HttpPost(postURL);
                        List<NameValuePair> crc = new ArrayList<NameValuePair>();
                        crc.add(new BasicNameValuePair("crc", "test"));
                        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(crc,HTTP.UTF_8);
                        post.setEntity(ent);
                        HttpResponse responsePOST = client.execute(post);  
                        entit = responsePOST.getEntity();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return entit;
            }
    }
}

And here is error log:

12-03 14:46:36.929: E/AndroidRuntime(3044): FATAL EXCEPTION: main
12-03 14:46:36.929: E/AndroidRuntime(3044): java.lang.RuntimeException: Unable to start activity ComponentInfo{app/app.MainActivity}: android.os.NetworkOnMainThreadException
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.os.Looper.loop(Looper.java:137)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at java.lang.reflect.Method.invoke(Method.java:511)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at dalvik.system.NativeStart.main(Native Method)
12-03 14:46:36.929: E/AndroidRuntime(3044): Caused by: android.os.NetworkOnMainThreadException
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:175)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.impl.io.ChunkedInputStream.exhaustInputStream(ChunkedInputStream.java:289)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.impl.io.ChunkedInputStream.close(ChunkedInputStream.java:262)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.conn.BasicManagedEntity.streamClosed(BasicManagedEntity.java:179)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:266)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:213)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at java.io.InputStreamReader.close(InputStreamReader.java:145)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.util.EntityUtils.toString(EntityUtils.java:139)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at org.apache.http.util.EntityUtils.toString(EntityUtils.java:146)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at pl.lodz.uni.uniinformator.MainActivity.onCreate(MainActivity.java:43)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.app.Activity.performCreate(Activity.java:4465)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-03 14:46:36.929: E/AndroidRuntime(3044):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-03 14:46:36.929: E/AndroidRuntime(3044):     ... 11 more

If I understand correctly method doInBackground should do everything what’s inside as it would be in another thread.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:56:08+00:00Added an answer on June 15, 2026 at 8:56 am

    – Its always better practice to have the UI work on UI-Thread and Non-UI work on Non-UI Thread, but from HoneyComb version of Android it became a Law.

    – In your case get() is a blocking call, which you are calling from UI thread, where as it should be on the Non-UI thread.

    – EntityUtils.toString(resEntity) should be in the Non-UI thread, so it should be in the doInBackground() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.