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 8143385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:06:41+00:00 2026-06-06T13:06:41+00:00

I have a map that i want to update from a separate thread. Im

  • 0

I have a map that i want to update from a separate thread. Im having some issues when i try to update the UI from my class that extends asynctask. And when i move the code to a handler in the main thread i get NetworkOnMainThreadException. Below is my asynctask. Is there any way to modify it to make it work?

@Override
    protected Integer doInBackground(Void... params) {

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://83.xx.xx:8080/android/service.php");

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("action", "getusers"));
            nameValuePairs.add(new BasicNameValuePair("interval", Integer.toString(interval)));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);

            String jsonString = EntityUtils.toString(response.getEntity());

            Gson gson = new GsonBuilder().create();

            Type listType = new TypeToken<ArrayList<User>>() {}.getType();
            ArrayList<User> userList = gson.fromJson(jsonString, listType);

            ((GoogleMapsActivity) activity).removeOverlayItems();

            for(User user : userList ){

                ((GoogleMapsActivity) activity).addOverlayItem(Double.parseDouble(user.last_lat), Double.parseDouble(user.last_lng), user.bluetooth_name, "test desc");
            }


            return 1;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
  • 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-06T13:06:42+00:00Added an answer on June 6, 2026 at 1:06 pm

    Rather than touching the map in your doInBackground, return your data into onPostExecute. Then update the map from onPostExecute.

    Please note that you should never retain a reference to your Activity from inside your AsyncTask or you could cause a memory leak.

    Ideally, you should create an observer that can return data to your Activity.

    interface Observer {
        public void onMyDataAvailable(MyDataClass data);
    }
    

    Then, you instantiate your AsyncTask with a reference to this observer.

    class MyAsyncTask extends AsyncTask ...
        private Observer observer;
    
        public MyAsyncTask(Observer observer) {
            mObserver = observer;
        }
    
        public synchronized void onActivityDestroyed() {
            mObserver = null;
        }
    
        public synchronized void onPostExecute(MyDataClass result) {
            if (mObserver != null) {
                mObserver.onMyDataAvailable(result);
            }
        }
    

    Just make sure you call onActivityDestroyed() from your activity’s onDestroy method. Otherwise your Activity will leak.

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

Sidebar

Related Questions

I have a map that represents a DB object. I want to get 'well
I have a Map, that I want to persist. The domain object is something
I have Java Map (out of Strings and Ints) objects that I want to
I have one very general object that I want to map to a destination
I have a silverlight application that contains Bing map. I want when the user
I have a a map that looks like this: public class VerbResult { @JsonProperty("similarVerbs")
I have a Service that downloads a file from the internet. What I want
I'm new at this, but I have a Google V3 map that loads from
I have a high-resolution image that I want to use as a tiled map
I want to have a map that permits a tree-like behaviour. I want to

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.