如何使用kso​​ap2来调用Web服务

时间:2015-09-29 23:05:15

标签: java android web-services ksoap2

我一直在尝试使用kso​​ap2连接到带有andorid的w3schools tempconvert Web服务,但是每次调用方法时我得到的结果都是com.example.myproject.MyTask @

我使用的代码是

public class MyTask extends AsyncTask<String, Integer, String>{

private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static final String OPERATION_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

@Override
protected String doInBackground(String... params) {
    String response = null;
    SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
    Request.addProperty("Celsius", "1");
    //Request.addProperty("strCommandParameters", params[1]);



    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(Request);
    // Needed to make the internet call

    // Allow for debugging - needed to output the request

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;
    // this is the actual part that will call the webservice
    try {
        androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
    } catch (HttpResponseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Get the SoapResult from the envelope body.
    SoapObject result = (SoapObject) soapEnvelope.bodyIn;   
    response = result.getProperty(0).toString();    
    return response;
}

}

我使用

从我的onCreate方法调用它
MyTask myTask = new MyTask();
myTask.execute(new String[] {"Celsius", "1"}).toString()

(顺便说一下,我意识到将参数发送到方法是没有意义的,因为它们是在被调用的方法中设置的。)

1 个答案:

答案 0 :(得分:1)

******* MyTask Class ********

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.content.Context;
import android.os.AsyncTask;

public class MyTask extends AsyncTask<String, Integer, String> {
    private AsyncTaskCompleteListener callback;

    public MyTask(Context context, MainActivity mainActivity) {
        // TODO Auto-generated constructor stub
        callback = mainActivity;
    }

    private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
    private static final String OPERATION_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
    private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

    @Override
    protected String doInBackground(String... params) {
        String response = null;
        SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
        Request.addProperty("Celsius", "1");

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        // this is the actual part that will call the webservice
        try {
            androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Get the SoapResult from the envelope body.
        SoapObject result = (SoapObject) soapEnvelope.bodyIn;
        response = result.getProperty(0).toString();
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        callback.onTaskComplete(result);
        super.onPostExecute(result);
    }
}

******* AsyncTaskCompleteListener ******
创建一个新的独立接口

public interface AsyncTaskCompleteListener {
    public void onTaskComplete(String result);
}

******* ******** MainActivity
1.您的主要活动必须implements AsyncTaskCompleteListener
2.在主要活动中覆盖以下方法。

@Override
    public void onTaskComplete(String result) {
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
    }
  1. 使用

    调用MyTask类

    new MyTask(getApplicationContext(),MainActivity.this).execute();