Android 基于ksoap2的webservice请求的学习

移动开发 作者: 2024-08-24 17:50:01
【学习阶段】 WebService网络请求? 其实我也是第一次遇到,之所以有这个需要是因为一些与 ERP 相关的业务,需要用到这样的一个请求方式。 开始学习WebService ①当然是百度搜索,这里

 【学习阶段】

  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();

 【开发阶段】

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_67946.html