如何使用 ksoap2 调用网络服务
How to use ksoap2 to call a web service
我一直在尝试使用 ksoap2 通过 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()
(顺便说一句,我意识到将参数发送到方法是没有意义的,因为它们是在被调用的方法中设置的。)
*******我的任务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. 你的主要 activity 必须 implements AsyncTaskCompleteListener
2. 在你的 main activity.
重写下面的方法
@Override
public void onTaskComplete(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
调用 MyTask class 使用
new MyTask(getApplicationContext(), MainActivity.this).execute();
我一直在尝试使用 ksoap2 通过 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()
(顺便说一句,我意识到将参数发送到方法是没有意义的,因为它们是在被调用的方法中设置的。)
*******我的任务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. 你的主要 activity 必须 implements AsyncTaskCompleteListener
2. 在你的 main activity.
@Override
public void onTaskComplete(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
调用 MyTask class 使用
new MyTask(getApplicationContext(), MainActivity.this).execute();