AsyncTask仅适用于第一次

时间:2011-06-09 02:57:29

标签: android asynchronous android-asynctask

我正在尝试创建一个实用程序httpClient类

我的代码在第一次执行时效果很好。在调试时,第二个执行不会进入或执行,所以有些事情搞砸了。

活动/听众

    protected String getPage(String url, List<NameValuePair> namevaluePairs, String postOrGet, Activity whichActivity, String dialogText) {
    try {
        httpHelper.setListValues(namevaluePairs);
        httpHelper.setPostOrGet(postOrGet);
        httpHelper.setParentActivity(whichActivity);
        httpHelper.setDialogText(dialogText);
        httpHelper.execute(url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultHTML;
}

Utility类:

    public class HTTPHelper extends AsyncTask<String, Void, Void> {
    private String resultString;
    private HttpClient httpclient;
    private List<NameValuePair> nameValuePairs;
    private String postOrGet;
    private Activity parentActivity;
    private String Error;
    private String dialogText;
    private ProgressDialog Dialog;
    WebServiceListener listener;

    public HTTPHelper(WebServiceListener listener) {
        this.listener = listener;
        Error = null;
        httpclient = new DefaultHttpClient();
        postOrGet = "get";
        nameValuePairs = null;
        dialogText = "Logging in";
    }

    @Override
    public void onPreExecute() {
        super.onPreExecute();
        Error = null;
        Dialog.setMessage(dialogText);
        Dialog.show();
    }

    @Override
    public void onPostExecute(Void unused) {
        Dialog.dismiss();
        if (Error != null) {
            Toast.makeText(parentActivity, Error, Toast.LENGTH_LONG).show();
        } else {
            ArrayList<String> myList = new ArrayList<String>();
            myList.add(resultString);
            listener.onHTTPGetComplete(myList);
        }
    }

    public void setDialogText(String txt) {
        dialogText = txt;
    }

    public void setListValues(List<NameValuePair> incNameValuePairs) {
        nameValuePairs = incNameValuePairs;
    }

    public void setPostOrGet(String pOrG) {
        postOrGet = pOrG;
    }

    public void setParentActivity(Activity myAct) {
        parentActivity = myAct;
        Dialog = new ProgressDialog(parentActivity);
    }

    @Override
    protected Void doInBackground(String... urls) {
        BufferedReader in = null;
        try {
            HttpGet httpget = new HttpGet(urls[0]);
            HttpPost httppost = new HttpPost(urls[0]);
            HttpResponse response = null;

            if (postOrGet.toLowerCase().contains("post")) {
                httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
                try {
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
                    response = httpclient.execute(httppost);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    response = httpclient.execute(httpget);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            } catch (IOException e) {
                e.printStackTrace();
            }

            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            try {
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            resultString = sb.toString();
            return null;
        } finally {
        }
    }
}

2 个答案:

答案 0 :(得分:2)

AsyncTask只能按文档中所述运行一次:http://developer.android.com/reference/android/os/AsyncTask.html

因此,对于每个请求,您都需要创建一个新的辅助对象。

编辑:

检查出来:ThreadSafeClientConnManager

答案 1 :(得分:0)

每次执行时,都会创建AsyncTaskHTTPHelper的新实例)。

相关问题