[英]how to use asynctask properly with ksoap2 and axis2 web service
[英]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()
(順便說一句,我意識到將參數發送給方法是沒有意義的,因為它們是在被調用的方法中設置的。)
*******我的任務類********
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);
}
}
*******異步任務完成監聽器******
創建一個新的獨立接口
public interface AsyncTaskCompleteListener {
public void onTaskComplete(String result);
}
*******主要活動********
1.你的主要活動必須implements AsyncTaskCompleteListener
2. 在您的主要活動中覆蓋以下方法。
@Override
public void onTaskComplete(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
使用調用 MyTask 類
new MyTask(getApplicationContext(), MainActivity.this).execute();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.