我是android新手。 我必须使用ksoap2从android客户端调用基于java的wsdl webservice。 谁能帮我? 我试着调用但得到XmlPullParserException。
答案 0 :(得分:0)
这是我使用ksoap
的类import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.os.AsyncTask;
import android.util.Log;
import searchparams.MetaData;
public class RequestTools {
public interface ResultListener{
/**
*
* @param result
* @param mode also used for error
*/
public void onReadable(String result, int mode);
}
private static KSOAPAsync mSearchAsync = null;
public static final int ERROR_UNDEFINED = -1;
public static final int MODE_CALCULATOR = 0;
public static final int MODE_WEATHER = 1;
public static void startGetWeatherData(ResultListener listener, String city){
mSearchAsync = new KSOAPAsync(listener, MODE_WEATHER);
mSearchAsync.execute(city);
}
public static void startGetAddition(ResultListener listener, String x, String y){
mSearchAsync = new KSOAPAsync(listener,MODE_CALCULATOR);
mSearchAsync.execute(x,y);
}
public static class KSOAPAsync
extends AsyncTask<String, Boolean, SoapPrimitive> {
private ResultListener resultListener = null;
private int modeType = -1;
public KSOAPAsync(ResultListener listener, int mode) {
resultListener = listener;
modeType = mode;
}
@Override
protected SoapPrimitive doInBackground(String... params) {
SoapPrimitive result = null;
switch(modeType){
case RequestTools.MODE_CALCULATOR:
result = getCalculation(
Integer.valueOf(params[0]),
Integer.valueOf(params[1]));
break;
case RequestTools.MODE_WEATHER:
result = getWeather(params[0]);
break;
default:
}
return result;
}
private SoapPrimitive getCalculation(int x, int y){
SoapObject request = new SoapObject(
MetaData.Calculator.NAMESPACE,
MetaData.Calculator.METHOD_NAME);
request.addProperty("x",x);
request.addProperty("y",y);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttptransport = new HttpTransportSE(
MetaData.Calculator.URL);
SoapPrimitive result = null;
try {
androidHttptransport.call(MetaData.Calculator.SOAP_ACTION,
envelope);
result = (SoapPrimitive)envelope.getResponse();
Log.i(TAG,""+result.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
private SoapPrimitive getWeather(String city){
SoapObject request = new SoapObject(
MetaData.Weather.NAMESPACE,
MetaData.Weather.METHOD_NAME);
request.addProperty("City", city);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttptransport = new HttpTransportSE(
MetaData.Weather.URL);
SoapPrimitive result = null;
try {
androidHttptransport.call(MetaData.Weather.SOAP_ACTION,
envelope);
result = (SoapPrimitive)envelope.getResponse();
Log.i(TAG,""+result.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
private static final String TAG = RequestTools.class.getSimpleName();
@Override
protected void onPostExecute(SoapPrimitive result) {
if(result == null){
resultListener.onReadable(null,RequestTools.ERROR_UNDEFINED);
}else{
resultListener.onReadable(result.toString(),modeType);
}
}
}
}