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

  • SEARCH
  • Home
  • 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 8609085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:47:20+00:00 2026-06-12T03:47:20+00:00

I want to grab some data from remote database and I have some networking

  • 0

I want to grab some data from remote database and I have some networking in my AsyncTask subclass of my activity, but when I try to load my program it shows NetworkOnMainThreadException.

here is my code:

public class MyMapLocationActivity extends MapActivity {

private MapView mapView;
private static String url_product_details = "http://myurl.com/readfromdb";
private static final String TAG_SUCCESS = "success";
private static final String TAG_LATITUDE = "latitude";
private static final String TAG_LONGITUDE = "longitude";
private static final String TAG_POINT = "point";

    JSONParser jsonParser = new JSONParser();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main); 

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    ReadLocations Read = new ReadLocations();
    Read.execute();
}

//And my networking AsyncTask:

class ReadLocations extends AsyncTask<String, String, String> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }


   @Override
    protected String doInBackground(String... args) {
       Log.d("debug", "START DOINBACKGROUND");


       runOnUiThread(new Runnable() {
           public void run() {
               // Check for success tag
               int success;
               try {

                   // Building Parameters
                   List<NameValuePair> params = new ArrayList<NameValuePair>();
                   params.add(new BasicNameValuePair("id", "16"));

                   // getting product details by making HTTP request
                   // Note that product details url will use GET request
                   Log.d("debug", "before http request");

                   JSONObject json = jsonParser.makeHttpRequest(
                           url_product_details, "POST", params);
                   Log.d("debug", "after http request");

                   // json success tag
                   success = json.getInt(TAG_SUCCESS);

                   if (success == 1) {
                       // successfully received product details
                       JSONArray productObj = json
                               .getJSONArray(TAG_POINT); // JSON Array

                       // get first product object from JSON Array
                       JSONObject product = productObj.getJSONObject(0);

                   }else{
                       // product with pid not found
                   }
               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }
       });

        return null; 
    }



  @Override
    protected void onPostExecute(String text) {
        // dismiss the dialog once done
   Log.d("debug", "ONPOSTEXECUTE");

    }

The LogCat shows “before http request” but doesn’t show my “after http request” check.

What is wrong?

  • 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-12T03:47:22+00:00Added an answer on June 12, 2026 at 3:47 am

    Your Network code is on MainUiThread of Application not in worker thread…

    remove

    runOnUiThread(new Runnable() {
               public void run() {
    

    from doInBackground()

    and put the WebRequest code only in doInBackground() of AsyncTask..

    Update:

    Actually your AsyncTask implementation for doInBackground() is wrong. As doInBackground() runs in worker thread for lengthy and network related operation. And you are trying to run MainUiThread form it using runOnUiThread() which is not in rule.. If you want to make any UI related work do it in onPostExecute(), onPreExecute() and other methods of ASyncTask.

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

Sidebar

Related Questions

I want to be able to occasionally programmatically grab some data from the graph
I want to grab my customers phone number from a MYSQL database and auto
I have a flash applet that I want to grab a remote XML file
The use case is that I have a remote (read slow) database from which
I need to copy some data from one table to another but I need
So I have a PHP backend that pulls some data from SQL, let's just
I need to grab some data from websites in my django website. Now i
I want to grab the most recent entry from a table. If I was
I want to grab from a db a record with name='John Doe'. I'd like
I want to grab records from a table based on the day of the

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.