简体   繁体   English

如何使用 ksoap2 调用 Web 服务

[英]How to use ksoap2 to call a web service

I have been trying to connect to the w3schools tempconvert web service with an andorid using ksoap2, however the result I get whenever calling a method is com.example.myproject.MyTask@我一直在尝试使用 ksoap2 使用 andorid 连接到 w3schools tempconvert Web 服务,但是每次调用方法时我得到的结果是 com.example.myproject.MyTask@

The code I have used is我使用的代码是

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;
}

} }

and I call this from my onCreate method using我从我的 onCreate 方法中调用它

MyTask myTask = new MyTask();
myTask.execute(new String[] {"Celsius", "1"}).toString()

(Btw I realise sending the parameters to the method is pointless because they are set in the called method.) (顺便说一句,我意识到将参数发送给方法是没有意义的,因为它们是在被调用的方法中设置的。)

*******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****** *******异步任务完成监听器******
Create a new separate Interface创建一个新的独立接口

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

*******MainActivity******** *******主要活动********
1. your main activity must implements AsyncTaskCompleteListener 1.你的主要活动必须implements AsyncTaskCompleteListener
2. override the below method in your main activity. 2. 在您的主要活动中覆盖以下方法。

@Override
    public void onTaskComplete(String result) {
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
    }
  1. call MyTask class using使用调用 MyTask 类

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM