直接上代码:
public class HttpUtil {
//wsdl的uri
private static final String URL = "http://x.x.x.x:x/Service/AppService.asmx?WSDL";
public static <T> void baseJSONObject(final Observer<JSONObject> observer, final SoapObject request){
Observable<T> oble = Observable.create(new ObservableOnSubscribe<T>() {
@Override
public void subscribe(@NonNull ObservableEmitter<T> e) throws Exception {
String response = post(request);
if(response != null){
System.out.println("baseJSONObject:" + response);
e.onNext((T) JSON.parseObject(response));
e.onComplete();
}else{
e.onError(new IOException());
}
}
}).subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observer oser = observer;
oble.subscribe(oser);
}
//调用webservice
public static String post(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
envelope.bodyOut = request;
envelope.dotNet = true;
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
try {
httpTransportSE.call(null, envelope);
} catch (Exception e) {
e.printStackTrace();
}
SoapObject object = (SoapObject) envelope.bodyIn;
String result = object.getProperty(0).toString();
return result;
}
}
我这里是定义了一个 HttpUtil 类,其中的 x.x.x.x:x就是你需要调用的ip地址和端口号了。
然后在你的自己定义的接口文件中定义你要调用的接口和参数,形式如下:
public static void function(Observer<JSONObject> observer, Type param, ...) {
SoapObject request = new SoapObject(NAMESPACE, "func");
request.addProperty("param", param); //传入参数,无参可省略这一步
baseJSONObject(observer, request);
}
其中的 NAMESPACE是我定义的一个类属性常量,在浏览器中输入
http://x.x.x.x:x/Service/AppService.asmx?WSDL
就会出现如下页面
<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://tempuri.org/">
...
倒数第二行的 targetNamespace 字段的值就是命名空间,而 func 就是你要真正调用的接口名啦!