1 package com.example.aust_app;
2
3
4 /*Created by wqy on 2019/11/8.*/
5
6 import android.content.Context;
7 android.os.AsyncTask;
8
9 org.ksoap2.SoapEnvelope;
10 org.ksoap2.serialization.SoapObject;
11 org.ksoap2.serialization.SoapSerializationEnvelope;
12 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 = 23
24
25 public WebRequest(Context context) {
26 this.context = context;
27 }
28
29 static WebRequest init(Context context){
30 return new WebRequest(context);
31 32
33 String getSOAP_ACTION() {
34 return SOAP_ACTION;
35 36
37 WebRequest setSOAP_ACTION(String SOAP_ACTION) {
38 this.SOAP_ACTION = 39 this 40 41
42 String getNAMESPACE() {
43 NAMESPACE;
44 45
46 WebRequest setNAMESPACE(String NAMESPACE) {
47 this.NAMESPACE = 48 49 50
51 String getMETHOD_NAME() {
52 METHOD_NAME;
53 54
55 WebRequest setMETHOD_NAME(String METHOD_NAME) {
56 this.METHOD_NAME = 57 58 59
60 String getURL() {
61 URL;
62 63
64 WebRequest setURL(String URL) {
65 this.URL = 66 67 68
69 private SoapObject getInformation(){
70 SoapObject request= SoapObject(NAMESPACE,METHOD_NAME);
71
72 SoapSerializationEnvelope envelope = SoapSerializationEnvelope(SoapEnvelope.VER11);
73 envelope.setOutputSoapObject(request);
74 try{
75 HttpTransportSE transportSE= 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 test;
80 }catch (Exception e){
81 e.printStackTrace();
82 }
83 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 93 94
95 96 protected void onPostExecute(Boolean aBoolean) {
97 StringBuilder builder = StringBuilder();
98 解析返回的数据
99 for(int i=0;i<result.getPropertyCount();i++){
100 builder.append(result.getProperty(i));
101 }
102 if (postExecute!=103 postExecute.getResult(builder.toString());
104 105 106 107
108 execute(){
109 DownloadTask().execute();
110 111
112 PostExecuteListener postExecute;
113 interface PostExecuteListener{
114 getResult(String result);
115 116
117 PostExecuteListener getPostExecute() {
118 postExecute;
119 120
121 WebRequest setPostExecuteListener(PostExecuteListener postExecute) {
122 this.postExecute =123 124 125 }
WebRequest.init(this).setURL("url").setNAMESPACE("namespace").setMETHOD_NAME("methodName")
.setSOAP_ACTION("soapAction").setPostExecuteListener( WebRequest.PostExecuteListener() {
@Override
getResult(String result) {
Toast.makeText(Main2Activity.this,""+result,Toast.LENGTH_SHORT).show();
}
}).execute();