Android开发调用webservice接口

直接上代码:

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 就是你要真正调用的接口名啦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值