Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use separate thread to perform http requests

I have an application that is performing HTTP Requests (specifically calling the FogBugz API) when the user clicks certain buttons. Right now, I am just creating a service when the application starts, and then calling different methods in that service to complete requests. However, when I do this, there is the usual hang in the UI thread. I have looked at AsyncTask, but am not sure it will do what I want to accomplish. Because I need to instantly parse the XML that the HTTP Request returns, I need to have a process that is able to return this data to the UI thread. Will ASyncTask be able to accomplish this, or is there some other way?

    public static InputStream makeRequest(String httpRequest)     {         In a separate thread, run HTTP Request, get back and process, return inputstream     } 

This method is called by several others to perform HttpRequests. Once the inputstream is returned, the other methods parse for specific information.

like image 537
mbauer14 Avatar asked Aug 02 '10 19:08

mbauer14


People also ask

Is HTTP request a thread?

An HTTP request is processed by a thread. A thread, in turn, can handle a number of network connections. You can specify the number of threads the Web server can process. In general, the number of threads specified is an indication of the number of users who can access the server simultaneously.


2 Answers

The simplest way to do it would just be to do something like

//Body of your click handler Thread thread = new Thread(new Runnable(){   @Override   public void run(){     //code to do the HTTP request   } }); thread.start(); 

That will cause the code inside the run() method to execute in a new thread of execution. You can look into an async task if you like although I've personally never used it. This is a quick and simple way to get things done.

With regards to passing information back, I would use a Handler object which effectively allows you to set up a message queue for a given thread and pass messages to it which cause the execution of specific code. The reason you need to do this is because Android will not let any thread other than the UI thread update the actual UI.

Does that address your question? I know my first pass didn't fully address all of your issues.

Edit Basically, what you do is define a handler object in your Activity like

private Handler handler_ = new Handler(){     @Override     public void handleMessage(Message msg){      }  }; 

You also create static int constants that help tell the handler what to do. You basically use those to allow for several different types of messages to be passed to one instance of a handler. If there is only going to be one message that is passed back, then you don't have to worry about that.

For example

private static final int UPDATE_UI = 1; 

To send a message to the handler you call

handler_.sendMessage(Message.obtain(handler_, UPDATE_UI, inputStreamInstance)); 

From the handler:

private Handler handler_ = new Handler(){     @Override public void handleMessage(Message msg){     switch(msg.what){             case UPDATE_UI:             InputStream is = (InputStream)msg.obj;             //do what you need to with the InputStream             break;         }     }  }; 

Alternatively, where the inputStreamInstance is added to the Message object, you can pass any object you like so if you wanted to parse it into some kind of container object or something like that, you could do that as well. Just cast it back to that object from within the handler.

like image 158
Chris Thompson Avatar answered Sep 27 '22 18:09

Chris Thompson


Try using AsyncTask. Goto this Link for more:

private class SyncIncoData extends AsyncTask<String, String, String> {     ProgressBar pb;     ProgressBar pbar;      @Override     protected String doInBackground(String... urls) {         for (int i = 0; i <= 100; i++) {             publishProgress(i);         }         return null;     }      @Override     protected void onPreExecute() {          super.onPreExecute();          pb = (ProgressBar) findViewById(R.id.progressBarsync4);         pb.setVisibility(View.VISIBLE);      }      @Override     protected void onPostExecute(String result) {          pb = (ProgressBar) findViewById(R.id.progressBarsync4);         pb.setVisibility(View.INVISIBLE);      }      @Override     protected void onProgressUpdate(String... values) {         pbar = (ProgressBar) findViewById(R.id.progressBarpbar);         pbar.setProgress(Integer.parseInt(values[0]));         super.onProgressUpdate(values);     } } 

Write the program for http request inside doinbackgroud()

like image 21
Jin Avatar answered Sep 27 '22 18:09

Jin