当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 基于ksoap2的webservice请求的学习

Android 基于ksoap2的webservice请求的学习

2019年11月13日  | 移动技术网移动技术  | 我要评论

 【学习阶段】

  • webservice网络请求?

    其实我也是第一次遇到,之所以有这个需要是因为一些与 erp 相关的业务,需要用到这样的一个请求方式。

  • 开始学习webservice 

  ①当然是百度搜索,这里找了一个学习的博客  

   使用 ksoap2 框架请求 ,jar 包下载地址  ,放在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();

 

 【开发阶段】

  以上是学习阶段,利用别人给的测试接口进行测试,并且进行一定的封装使用,一系列没有任何问题。然后再去进入自己开发阶段。【解决问题⬇

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网