使用 Retrofit 操作 SOAP Web Service
前一段儿时间转载了一篇使用Retrofit操作SOAp的文章,说实话我也没怎么看懂,还是一篇繁体字的文章,我简单的该了一下,改成了简体的。根据上面的方法也没有弄成功,可能是我水平不到,留给大家参考
今天我用自己的方法实现了我想要的效果,给大家分享一下,方法可能比较笨。
用普通的方式请求SOAP式的Web Service,说白了就是使用Http请求的方式进行请求,不过请求的的参数要使用SOAP定义的XMl格式的数据,每个公司定义的格式可能不同,刚开始一直找不到请求不成功的原因,各种调试,好在最好找到了解决方法,下面有详细的过程。
一、首先这是我用Chrome中的接口调试工具
调试的结果
1.service的URl大家都应该知道
2.头信息:Content_Type 设置为text/xml,应为SOAP的参数是XML格式的。 SOAPAction每个接口都不一样要看WebService的定义,下面把我的查看方式写在下面,仅供参考。找不到的问一下服务器开发人员。
打开Webservice的跟地址,如果地址正确就应该是这样的
点击图上的链接就能看到配置文件的页面
在网页上搜索你请求的方法名,就能找到对应的SOAPAction
下面就是最重要的寻找XML请求参数的格式了(如果可以最好向服务器开发人员索要,自己搞略显麻烦)
具体方法就是抓取请求接口时的网络请求包,然后在wireshark上查看具体参数
然后在wireshark中查看
到此为止所有需要的参数到拿到了。填写到接口调试工具中进行测试。如果不正确请联系服务器开发人员索要正确参数
二、如果你接口调试中返回了正确的数据那么我们可以开始第二个阶段,在Retrofit中调用WebService接口。其实知道了参数,用Retrofit进行Http调用就行了。不过有几点要注意
一个是请求的路径,还有一个是请求是Post方法,body参数是String类型的,retrofit中默认没有String类型的Body参数,需要自定义String类型的Coverter
如果打不开这个网站,可以使用下面的代码(注意使用的是Retrofit2.0),接口定义中使用Body注解,类型是String
public interface AuthenticationAPI {
@POST("token")
Call<String> authenticate(@Body String body);
}
Then I call:
Retrofit retrofit = new Retrofit.Builder().baseUrl(authenUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
AuthenticationAPI authen = retrofit.create(AuthenticationAPI.class);
try {
Call<String> call1 = authen.authenticate(authenBody);
call1.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response, Retrofit retrofit) {
Log.d("THAIPD", "Success " + response.raw().message());
}
@Override
public void onFailure(Throwable t) {
Log.e("THAIPD", " FAIL " + t.getMessage());
}
});
} catch (Exception e) {
Log.e("THAIPD", e.getMessage());
e.printStackTrace();
}
最后的接口定义是这样的(头信息的定义如下所示)
@Headers({
"Content-Type: text/xml",
"SOAPAction: http://tempuri.org/IJsonOperation/GetAllCardInfos"
})
@POST( "如果有子路径就写在这里")
Call<String> getJson( @Body String requestStr );
"Content-Type: text/xml",
"SOAPAction: http://tempuri.org/IJsonOperation/GetAllCardInfos"
})
@POST( "如果有子路径就写在这里")
Call<String> getJson( @Body String requestStr );
然后设置自定义的String类型的Converter
Retrofit restAdapter =
new Retrofit.Builder()
.baseUrl(
"http://192.168.23.22/")
.addConverterFactory(
new ToStringConverterFactory()
)
.build();
.baseUrl(
"http://192.168.23.22/")
.addConverterFactory(
new ToStringConverterFactory()
)
.build();
最后调用定以后啊的接口,并把XML的String穿进去,注意是String
gitapi git = restAdapter.create(gitapi.
class);
final String requestStr = "<v:Envelope xmlns:i= \" http://www.w3.org/2001/XMLSchema-instance \" xmlns:d= \" http://www.w3.org/2001/XMLSchema \" xmlns:c= \" http://schemas.xmlsoap.org/soap/encoding/ \" xmlns:v= \" http://schemas.xmlsoap.org/soap/envelope/ \" ><v:Header /><v:Body><GetAllCardInfos xmlns= \" http://tempuri.org/ \" id= \" o0 \" c:root= \" 1 \" ><clientName i:type= \" d:string \" >Monitor</clientName></GetAllCardInfos></v:Body></v:Envelope> \n ";
Call<String> call = git.getJson(requestStr);
final String requestStr = "<v:Envelope xmlns:i= \" http://www.w3.org/2001/XMLSchema-instance \" xmlns:d= \" http://www.w3.org/2001/XMLSchema \" xmlns:c= \" http://schemas.xmlsoap.org/soap/encoding/ \" xmlns:v= \" http://schemas.xmlsoap.org/soap/envelope/ \" ><v:Header /><v:Body><GetAllCardInfos xmlns= \" http://tempuri.org/ \" id= \" o0 \" c:root= \" 1 \" ><clientName i:type= \" d:string \" >Monitor</clientName></GetAllCardInfos></v:Body></v:Envelope> \n ";
Call<String> call = git.getJson(requestStr);
到此为止就完成了使用Retrofit调用SOAP类型的WebService。从完全不知道怎么弄,然后一点一点查资料,到最后调试成功,花了我两三天的时间,希望能对大家有所帮助,不要再经历我这样的痛苦三天。
好了就到此为止了,过程太复杂了,很不容易写清楚,所以有什么问题大家请留言