Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Http Connection using AsyncTask class?

Tags:

android

I am trying to create HTTP connection using AsyncTask class.

Is it possible to create HTTP connection ?

Can you suggest sample source code ?

Thanks in advance.

like image 200
Ferdinand Avatar asked Feb 05 '10 04:02

Ferdinand


2 Answers

As an inner class inside your activity :

public final class HttpTask
        extends
        AsyncTask<String/* Param */, Boolean /* Progress */, String /* Result */> {

    private HttpClient mHc = new DefaultHttpClient();

    @Override
    protected String doInBackground(String... params) {
        publishProgress(true);
        // Do the usual httpclient thing to get the result
        return result;
    }

    @Override
    protected void onProgressUpdate(Boolean... progress) {
        // line below coupled with 
        //    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS) 
        //    before setContentView 
        // will show the wait animation on the top-right corner
        MyActivity.this.setProgressBarIndeterminateVisibility(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        publishProgress(false);
        // Do something with result in your activity
    }

}

Then somewhere in your activity :

new HttpTask().execute(someParams...);
like image 173
Hubert Avatar answered Sep 18 '22 10:09

Hubert


i think this may help u...

http://androidbeginner.blogspot.com/2010/01/communication-with-httprequest.html

Atul yadav

like image 34
Atul Yadav Avatar answered Sep 18 '22 10:09

Atul Yadav