您的位置:首页 > 理论基础 > 计算机网络

How to create Http Connection using AsyncTask class?

2011-12-06 18:40 429 查看
问:

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.
回答:

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...);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: