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>

在調用這個方法之前,有很多準備工作需要做。

  • 校驗接口文檔中的信息和服務端實際信息是否一致

先看一下接口文檔

android 訪問webservice(解析一行代碼實現)

通過接口文檔我們瞭解我們需要的信息

1.接口請求方式 webservice

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

3.方法名:translateMacs

4.方法裡的2個參數,參數類型。

看完這些信息後,打開接口地址,檢查接口文檔中的信息和接口中的實際信息是否一致。

下圖中標註的地方都是需要關注的。

android 訪問webservice(解析一行代碼實現)

上圖標註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

結束了。以上信息僅供參考,因為接口不是公共接口。如果對你有益,記得點贊啊。


分享到:


相關文章: