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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:55:27+00:00 2026-05-31T09:55:27+00:00

Here is my problem: I need to do several requests on a server. These

  • 0

Here is my problem:

I need to do several requests on a server. These requests have to be made one after the other in order to avoid mixing. For that, I’m using monitors.

Here is what I’ve come up so far:

public class TestActivity extends Activity
{
  private String key;
  private HashMap<String, String> values;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    values = new HashMap<String, String>();

    ArrayList<String> list = new ArrayList<String>();
    list.add("foo");
    list.add("bar");
    list.add("baz");

    createValues(list);
  }

  private void createValues(final ArrayList<String> list)
  {
    Thread thread = new Thread(new Runnable() {
      @Override
      public void run()
      {
        key = null;
        for (String element : list)
        {
          if (key != null) // Every time except the first time.
          {
            synchronized (key)
            {
              try
              {
                key.wait();
              }
              catch (InterruptedException e)
              {
              }
            }
          }
          key = element;
          DataProcessor dataProcessor = new DataProcessor();
          dataProcessor.execute("Processed " + element);
        }
      }
    });
  }

  private void putDataInValue(String element)
  {
    synchronized (key)
    {
      values.put(key, element);
      key.notify();
    }
  }

  private class DataProcessor extends AsyncTask<String, Void, String>
  {
    @Override
    protected String doInBackground(String... params)
    {
      // Fetching data on a server. This takes time.
      try
      {
        Thread.sleep(10000);
      }
      catch (InterruptedException e)
      {
      }
      return params[0] + " from the server";
    }

    @Override
    protected void onPostExecute(String result)
    {
      putDataInValue(result);
    }
  }
}

What I would like after that is that the content of values is:

[
 "foo" => "Processed foo from the server",
 "bar" => "Processed bar from the server",
 "baz" => "Processed baz from the server"
]

I need to keep the values in the list and know which one corresponds to what content (hence the monitor).

My problem is that I keep getting an error message in my LogCat:

Can't create handler inside thread that has not called Looper.prepare()

I’ve searched the web, found some people who had that problem, looked at the answers, most of them saying I needed to use Handlers. Handlers don’t work either. I tried replacing the thread by

handler = new Handler(Looper.getMainLooper);
handler.post(new Runnable //...
//...

but it simply freezes.

I am ready to admit that my approach is wrong and start again from scratch if you think I’m in a dead end. What would you do ?

  • 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-05-31T09:55:28+00:00Added an answer on May 31, 2026 at 9:55 am

    You have probably already arrived at a solution for this, but in any case, here is one way you can enforce the sequential order of your requests using an Executor:

    public class SequentialTaskExampleActivity extends Activity {
    
        /**
         * will only execute one job at a time, in the order given to it.
         */
        private Executor executor = Executors.newSingleThreadExecutor();
        private Map<String, String> values = new HashMap<String, String>();
        private TextView textView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            textView = new TextView(this);
            textView.setTextSize(24);
            setContentView(textView);
    
            // Initialise jobs and add to queue 
            ArrayList<String> list = new ArrayList<String>();
            list.add("foo");
            list.add("bar");
            list.add("baz");
    
            for (String key : list) {
                executor.execute(new Job(key));
            }
        }
    
        public void addToResult(String key, String value) {
            values.put(key, value);
    
            // display result to UI
            textView.setText(String.format("%s %s => %s\n", textView.getText(), key, value));
        }
    
        private class Job implements Runnable {
            private String key;
    
            public Job(String key) {
                this.key = key;
            }
    
            @Override
            public void run() {
                // simulate work
                try {
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                // retrieve result
                final String value = key + " from the server";
    
                // post result back to UI
                runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        addToResult(key, value);
                    }
                });
            }
        }
    }
    

    If you are targeting API 11+ you can specify a particular executor to use with your AsyncTask instead (in fact, I think the default new behaviour is serial processing). In any case, you should always create and execute the AsyncTask on the UI thread; this is the only way to ensure AsyncTask.onPostExecute() behaves correctly.

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

Sidebar

Related Questions

I have an interesting SQL problem that I need help with. Here is the
I need help with CMD scripts. Here is my problem: I have list of
I need a little push in the right direction. Here's my problem: I have
Ok, so here's the problem I have to solve. I need to write a
This is my first post here. I have a problem. I need to take
I have a problem with my code. I need to do several operations on
Here's the problem, I need to validate the form before submitting in the next
Here is the problem... For school project I need to write parallel application using
Well i need some help here i don't know how to solve this problem.
Here's my problem. I am calling a service that is returning several identical nodes

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.