06.24 android 访问webservice(解析一行代码实现)

本文主要讲如何和webservice交互,至于一行代码实现,是因为调用了下面的方法。哈哈,意不意外

/**
* 请求webservice的步骤
* @param wsdl_url wsdl 的uri,接口地址 类似于这种:http://47.95.217.28:8080/services/translateMacs?wsdl
* @param name_sapce 命名空间 在wsdl_url链接里面有命名空间相关信息。
* @param methodName 方法名
* @param map 传给webservice的参数。
* @return
*/
public static String questToWebService(String wsdl_url,String name_sapce,String methodName,HashMap<string> map) {
String result = "";
//(1)创建HttpTransportSE对象,该对象用于调用WebService操作
HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI,90*1000);
//(2)创建SoapSerializationEnvelope对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
//(3)创建SoapObject对象,创建该对象时需要传入所要调用的Web Service的命名空间和WebService方法名
SoapObject request = new SoapObject(NAME_SPACE, methodName);
//(4) //填入需要传入的参数
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()){
String key = (String) iterator.next();
String value = map.get(key);
request.addProperty(key,value);
}
//(5)调用SoapSerializationEnvelope的setOutputSoapObject()方法,或者直接对bodyOut属性赋值,
//将前两步创建的SoapObject对象设为SoapSerializationEnvelope的传出SOAP消息体
envelope.bodyOut = request;
// envelope.setOutputSoapObject(request);
try {
//(6)调用对象的call()方法,并以SoapSerializationEnvelope作为参数调用远程的web service
httpTransportSE.call(methodName, envelope);//调用
if (envelope.getResponse() != null) {
result = envelope.getResponse().toString().trim();
}


return result;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
//解析该对象,即可获得调用web service的返回值
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

41
42
43
44
/<string>

在调用这个方法之前,有很多准备工作需要做。

校验接口文档中的信息和服务端实际信息是否一致

先看一下接口文档

通过接口文档我们了解我们需要的信息

1.接口请求方式 webservice

2.接口请求地址:http://47.95.217.28:8080/services/translateMacs?wsdl

3.方法名:translateMacs

4.方法里的2个参数,参数类型。

看完这些信息后,打开接口地址,检查接口文档中的信息和接口中的实际信息是否一致。

下图中标注的地方都是需要关注的。

上图标注1对应questToWebService方法中的nameSpace,

标注2对应questToWebService方法中的SoapEnvelope.VER10,

标注3对应questToWebService方法中的methodName,

标注4封住为questToWebService方法中的map。

确保以上信息和接口文档中的信息对应后,进行下一步操作。

准备questToWebService方法中的参数信息。 获取Mac列表,并用Gson转为json字符串。

/**
* 模拟数据,获取mac列表
* @return
*/
private String getMacs() {
List<hashmap>> macs = new ArrayList<>();
HashMap<string> map = new HashMap<>();
map.put("Mac","353765072974752");
macs.add(map);
map = new HashMap<>();
map.put("Mac","353765072974753");
macs.add(map);
return mGson.toJson(macs);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/<string>/<hashmap>

将接口中需要的请求参数封装到map中

/**
* 将接口需要的所有参数封装到hashmap中
* @return


*/
private HashMap<string> getRequestHashMapFortranslateMacs() {
HashMap<string> map = new HashMap<string>();
map.put("macsListc", getMacs());
map.put("requestTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
return map;
}
1
2
3
4
5
6
7
8
9
10
11
12
/<string>/<string>/<string>

至此准备工作完成了,

调用和webservice交互的方法

String wsdl_url = "http://47.95.217.28:8080/services/translateMacs?wsdl";
String name_sapce = "http://webservice.observer.com/";
questToWebService(wsdl_url,name_sapce,"translateMacs", getRequestHashMapFortranslateMacs());
1
2
3

结束了。以上信息仅供参考,因为接口不是公共接口。如果对你有益,记得点赞啊。