0

我正在使用肥皂方法发送请求。然后我将响应存储在 doInBackground 中的 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("");
        }
    }
4

1 回答 1

0

你如何声明你的 AsyncTask 类?

根据AsyncTask 文档

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

Params,执行时发送给任务的参数类型。进度,在后台计算期间发布的进度单元的类型。Result,后台计算结果的类型。并非所有类型都始终由异步任务使用。要将类型标记为未使用,只需使用类型 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("");
        }
    }
}
于 2013-05-29T07:25:24.043 回答