【學習階段】 WebService網路請求? 其實我也是第一次遇到,之所以有這個需要是因為一些與 ERP 相關的業務,需要用到這樣的一個請求方式。 開始學習WebService ①當然是百度搜索,這裡找了一個學習的博客 https://blog.csdn.net/swjtugiser/article ...
【學習階段】
-
WebService網路請求?
其實我也是第一次遇到,之所以有這個需要是因為一些與 ERP 相關的業務,需要用到這樣的一個請求方式。
-
開始學習WebService
①當然是百度搜索,這裡找了一個學習的博客 https://blog.csdn.net/swjtugiser/article/details/76840353
使用 ksoap2 框架請求 ,jar 包下載地址 http://simpligility.github.io/ksoap2-android/getting-started.html ,放在Project 模式的 libs 目錄下。
根據以上地址進行學習。
②在開發的過程中我們頻繁的用到 網路請求,所以我們最好能封裝成像okhttp一樣的類。
1 package com.example.aust_app;
2
3
4 /*Created by wqy on 2019/11/8.*/
5
6 import android.content.Context;
7 import android.os.AsyncTask;
8
9 import org.ksoap2.SoapEnvelope;
10 import org.ksoap2.serialization.SoapObject;
11 import org.ksoap2.serialization.SoapSerializationEnvelope;
12 import org.ksoap2.transport.HttpTransportSE;
13
14 public class WebRequest {
15
16 private String SOAP_ACTION="http://WebXml.com.cn/getRegionProvince"; //可以設置一些預設值
17 private String NAMESPACE="http://WebXml.com.cn/";
18 private String METHOD_NAME="getRegionProvince";
19 private String URL="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL";
20
21 WebRequest request = null;
22 Context context = null;
23
24
25 public WebRequest(Context context) {
26 this.context = context;
27 }
28
29 public static WebRequest init(Context context){
30 return new WebRequest(context);
31 }
32
33 public String getSOAP_ACTION() {
34 return SOAP_ACTION;
35 }
36
37 public WebRequest setSOAP_ACTION(String SOAP_ACTION) {
38 this.SOAP_ACTION = SOAP_ACTION;
39 return this;
40 }
41
42 public String getNAMESPACE() {
43 return NAMESPACE;
44 }
45
46 public WebRequest setNAMESPACE(String NAMESPACE) {
47 this.NAMESPACE = NAMESPACE;
48 return this;
49 }
50
51 public String getMETHOD_NAME() {
52 return METHOD_NAME;
53 }
54
55 public WebRequest setMETHOD_NAME(String METHOD_NAME) {
56 this.METHOD_NAME = METHOD_NAME;
57 return this;
58 }
59
60 public String getURL() {
61 return URL;
62 }
63
64 public WebRequest setURL(String URL) {
65 this.URL = URL;
66 return this;
67 }
68
69 private SoapObject getInformation(){
70 SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME);
71
72 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
73 envelope.setOutputSoapObject(request);
74 try{
75 HttpTransportSE transportSE=new HttpTransportSE(URL);
76 transportSE.call(SOAP_ACTION,envelope);
77 SoapObject result=(SoapObject)envelope.bodyIn; //獲取到返回的結果,並強制轉換成SoapObject對象
78 SoapObject test = (SoapObject)result.getProperty(0); //該對象中還嵌套了一個SoapObject對象,需要使用getProperty(0)把這個對象提取出來
79 return test;
80 }catch (Exception e){
81 e.printStackTrace();
82 }
83 return null;
84 }
85
86 SoapObject result; //在子線程中請求webservice
87 class DownloadTask extends AsyncTask<Void,Integer,Boolean> {
88
89 @Override
90 protected Boolean doInBackground(Void... voids) {
91 result = getInformation();
92 return null;
93 }
94
95 @Override
96 protected void onPostExecute(Boolean aBoolean) {
97 StringBuilder builder = new StringBuilder();
98 //解析返回的數據
99 for(int i=0;i<result.getPropertyCount();i++){
100 builder.append(result.getProperty(i));
101 }
102 if (postExecute!=null){
103 postExecute.getResult(builder.toString());
104 }
105 }
106 }
107
108 public void execute(){
109 new DownloadTask().execute();
110 }
111
112 PostExecuteListener postExecute;
113 interface PostExecuteListener{
114 void getResult(String result);
115 }
116
117 public PostExecuteListener getPostExecute() {
118 return postExecute;
119 }
120
121 public WebRequest setPostExecuteListener(PostExecuteListener postExecute) {
122 this.postExecute = postExecute;
123 return this;
124 }
125 }
③在Activity中使用這樣的類
WebRequest.init(this).setURL("url").setNAMESPACE("namespace").setMETHOD_NAME("methodName")
.setSOAP_ACTION("soapAction").setPostExecuteListener(new WebRequest.PostExecuteListener() {
@Override
public void getResult(String result) {
Toast.makeText(Main2Activity.this, ""+result, Toast.LENGTH_SHORT).show();
}
}).execute();
【開發階段】
以上是學習階段,利用別人給的測試介面進行測試,並且進行一定的封裝使用,一系列沒有任何問題。然後再去進入自己開發階段。【解決問題⬇