如何使用AsyncTask类创建Http连接? [英] How to create Http Connection using AsyncTask class?

查看:74
本文介绍了如何使用AsyncTask类创建Http连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AsyncTask类创建HTTP连接.

I am trying to create HTTP connection using AsyncTask class.

是否可以创建HTTP连接?

Is it possible to create HTTP connection ?

您能建议示例源代码吗?

Can you suggest sample source code ?

谢谢.

推荐答案

作为活动中的内部类:

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...);

这篇关于如何使用AsyncTask类创建Http连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆