0

我有一个旨在访问我的 Web 服务方法的类。问题是当我试图在异步调用中访问我的 Web 服务方法的输出时,它是空的。我应该怎么办?
这是我的网络服务类:

package ClassLibrary;

public class WebService {
    private String namespace="";
    private String url="";

    public WebService(String namespace,String url) {
        super();
        this.namespace = namespace;
        this.url = url;
    }

    public String CallMethod(String methodName,PropertyInfo pi) {
        String result = "default";
        SoapObject request = new SoapObject(namespace, methodName);
        request.addProperty(pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
        try {
            androidHttpTransport.call(namespace+methodName, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            result= response.toString();

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

这就是我试图调用网络服务的方式:

private class AsyncCallWS extends AsyncTask<String, Void, Void> {

    private ProgressDialog dialog;
    private Activity activity;
    public String wsOutput="";
    public String methodName="";
    private WebService ws;

    public AsyncCallWS(Activity activity,String methodName) {
        this.activity = activity;
        this.dialog = new ProgressDialog(activity);
        this.methodName = methodName;
    }

    @Override
    protected Void doInBackground(String... params) {
        ws = new WebService(PublicVariable.NAMESPACE, PublicVariable.URL);
        PropertyInfo pi= new PropertyInfo();
        pi.setName("UserID");
        pi.setValue("1");
        pi.setType(String.class);
        wsOutput=ws.CallMethod("GetPersonalInfo", pi);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }

        if (methodName == "GetPersonalInfo") {
            Log.d("Ehsan","OUTPUT IS:"+ wsOutput);
        }
    }
}
4

1 回答 1

1

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

  • Params,执行时发送给任务的参数类型。

  • 进度,在后台计算期间发布的进度单元的类型。

  • Result,后台计算结果的类型。

onPostExecute(Result)在后台计算完成后在 UI 线程上调用。后台计算的结果作为参数传递给此步骤,而您没有传递结果值。

private class AsyncCallWS extends AsyncTask<String, Void, String> {

    private ProgressDialog dialog;
    private Activity activity;
    public String methodName="";
    private WebService ws;

    public AsyncCallWS(Activity activity,String methodName) {
        this.activity = activity;
        this.dialog = new ProgressDialog(activity);
        this.methodName = methodName;
    }

    @Override
    protected Void doInBackground(String... params) {
        ws = new WebService(PublicVariable.NAMESPACE, PublicVariable.URL);
        PropertyInfo pi= new PropertyInfo();
        pi.setName("UserID");
        pi.setValue("1");
        pi.setType(String.class);

        String wsOutput = ws.CallMethod("GetPersonalInfo", pi);

        return wsOutput;
    }

    @Override
    protected void onPostExecute(String result) {

        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }

        Log.d("Ehsan","OUTPUT IS:"+ result);
    }
}
于 2013-10-30T10:02:46.767 回答