AsyncTask 仅在第一次有效

发布于 2024-11-14 20:58:02 字数 3988 浏览 3 评论 0原文

我正在尝试创建一个实用程序 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;
}

实用程序类:

    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 {
        }
    }
}

I'm trying to make a utility httpClient class

My code works great the first execute. When debugging, the second execute won't step in, or execute, so something is messed up.

The Activity/Listener

    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;
}

The Utility class:

    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 {
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘和柠 2024-11-21 20:58:02

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

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

编辑:

看看这个: ThreadSafeClientConnManager

An AsyncTask can only be run once as stated in the documentation : http://developer.android.com/reference/android/os/AsyncTask.html

So for every request you need to create a new helper object.

Edit:

Check this out : ThreadSafeClientConnManager

口干舌燥 2024-11-21 20:58:02

每次执行时都会创建一个新的 AsyncTask 实例(在您的情况下为 HTTPHelper)。

Create a new instance of the AsyncTask (HTTPHelper in your case) each time you execute it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文