Android AsyncTask仅在第一次工作时有效

Android AsyncTask仅在第一次工作时有效,android,asynchronous,android-asynctask,Android,Asynchronous,Android Asynctask,我正在尝试创建一个实用程序httpClient类 我的代码在第一次执行时运行良好。在调试时,第二次执行不会介入或执行,因此会出现问题 活动/侦听器 protected String getPage(String url, List<NameValuePair> namevaluePairs, String postOrGet, Activity whichActivity, String dialogText) { try { httpHelper.s

我正在尝试创建一个实用程序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;
}
受保护的字符串getPage(字符串url、列表名称值对、字符串postOrGet、活动whichActivity、字符串对话框文本){
试一试{
httpHelper.setListValues(namevaluePairs);
httpHelper.setPostOrGet(postOrGet);
httpHelper.setParentActivity(whichActivity);
httpHelper.setDialogText(dialogText);
httpHelper.execute(url);
}捕获(例外e){
e、 printStackTrace();
}
返回结果tml;
}
实用程序类:

    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 {
        }
    }
}
公共类HTTPHelper扩展异步任务{
私有字符串resultString;
私有HttpClient HttpClient;
私人名单;
私有字符串postOrGet;
私人活动;
私有字符串错误;
私有字符串对话框文本;
私人对话;
WebServiceListener侦听器;
公共HTTPHelper(WebServiceListener侦听器){
this.listener=listener;
错误=null;
httpclient=新的DefaultHttpClient();
postOrGet=“get”;
nameValuePairs=null;
dialogText=“登录”;
}
@凌驾
公共无效onPreExecute(){
super.onPreExecute();
错误=null;
设置消息(dialogText);
Dialog.show();
}
@凌驾
公共void onPostExecute(void unused){
Dialog.dismise();
if(错误!=null){
Toast.makeText(parentActivity,Error,Toast.LENGTH_LONG).show();
}否则{
ArrayList myList=新的ArrayList();
添加(结果字符串);
onHTTPGetComplete(myList);
}
}
公共void setDialogText(字符串txt){
dialogText=txt;
}
public void setListValues(列表incNameValuePairs){
nameValuePairs=incNameValuePairs;
}
公共void setPostOrGet(字符串端口){
postOrGet=pOrG;
}
公共活动(活动myAct){
parentActivity=myAct;
Dialog=新建进度对话框(parentActivity);
}
@凌驾
受保护的Void doInBackground(字符串…URL){
BufferedReader in=null;
试一试{
HttpGet HttpGet=新的HttpGet(URL[0]);
HttpPost HttpPost=新的HttpPost(URL[0]);
HttpResponse响应=null;
if(postOrGet.toLowerCase()包含(“post”)){
setHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
response=httpclient.execute(httppost);
}捕获(IOE异常){
e、 printStackTrace();
}
}否则{
试一试{
response=httpclient.execute(httpget);
}捕获(IOE异常){
e、 printStackTrace();
}
}
试一试{
in=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
}捕获(IOE异常){
e、 printStackTrace();
}
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
字符串NL=System.getProperty(“line.separator”);
试一试{
而((line=in.readLine())!=null){
sb.追加(行+NL);
}
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
in.close();
}捕获(IOE异常){
e、 printStackTrace();
}
resultString=sb.toString();
返回null;
}最后{
}
}
}

异步任务只能运行一次,如文档中所述:

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

编辑:


检查以下内容:

每次执行时创建一个新的
AsyncTask
HTTPHelper
)。谢谢拉维和阿帕扎德。我会投票给你们两个,但我的分数还不够。@user763472你应该接受一个好的答案,而不仅仅是投票。我认为你不需要任何分数来接受你问题的答案。接受答案会鼓励人们帮助你。嗨,拉维,我也做过类似的事情,但它不起作用-
public void onClick(View v){int index=groupPos;//现在读取相应的mChildStates字段并处理数据。boolean chBoxesState[]=mChildCheckStates.get(index);//将此数据发送到后端Log.d(“单击Vinit”,“我在这里”);RequestCloser reqCloser=null;reqCloser=new RequestCloser();reqCloser.execute(chBoxesState);}
您能帮我一下吗