AsyncTask清除onPostExecute中的响应

时间:2013-05-27 08:36:10

标签: android-asynctask

我正在使用肥皂法发送请求。然后我在doInBackground中存储Response in String变量,然后检查条件onPostExecute。 但是当我逐步检查它时,我才知道它正在清除值onPostExecute。 如何解决这个问题呢?

protected Void doInBackground(Void... args) {

        request = new SoapObject(NAMESPACE, METHOD_NAME);

        username = new PropertyInfo();
        username.setName("UserName");
        username.setValue(Username);
        username.setType(String.class);
        request.addProperty(username);

        password = new PropertyInfo();
        password.setName("Password");
        password.setValue(Password);
        password.setType(String.class);
        request.addProperty(password);

        SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envp.dotNet = true;
        envp.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
            androidHttpTransport.call(SOAP_ACTION, envp);
            SoapPrimitive response = (SoapPrimitive) envp.getResponse();
            Response = response.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }

        if (Response.equals("Fail")) {
            etPassword.setText("");
            textValidation.setText("UserName Or Password is Wrong.");

        } else {
            etUsername.setText("");
            etPassword.setText("");
        }
    }

1 个答案:

答案 0 :(得分:0)

您如何申报AsyncTask课程?

根据AsyncTask documentation

  

异步任务使用的三种类型如下:

     

Params,执行时发送给任务的参数类型。   进展,期间发布的进度单位的类型   背景计算。结果,结果的类型   背景计算。并非所有类型都始终使用   异步任务。要将类型标记为未使用,只需使用类型Void。

应该是这样的:

private class MyAsyncTask extends AsyncTask<Param, Progress, Result>

这将定义doInBackground()签名如下:

protected Result doInBackground(Param... params)

然后,您可以在doInBackground()而不是null的末尾返回结果。

修改:

应用于您的示例,它应该看起来像这样:

public class MyAsyncTask extends AsyncTask<Void, int[], String> {

    protected Void doInBackground(Void... args) {

        request = new SoapObject(NAMESPACE, METHOD_NAME);

        username = new PropertyInfo();
        username.setName("UserName");
        username.setValue(Username);
        username.setType(String.class);
        request.addProperty(username);

        password = new PropertyInfo();
        password.setName("Password");
        password.setValue(Password);
        password.setType(String.class);
        request.addProperty(password);

        SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
        envp.dotNet = true;
        envp.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envp);
            SoapPrimitive response = (SoapPrimitive) envp.getResponse();
            // return your result here
            return response.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        // return null if something went wrong
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }

        if (result == null) {
            // handle null here, for example :
            // result = "Fail";
        }

        // use "Fail".equals(result) instead of result.equals("Fail")
        // to avoid error if result is null
        if ("Fail".equals(result)) { 
            etPassword.setText("");
            textValidation.setText("UserName Or Password is Wrong.");

        } else {
        etUsername.setText("");
        etPassword.setText("");
        }
    }
}