WebSerivce封装

来源:互联网 发布:网络约车平台架构图 编辑:程序博客网 时间:2024/06/11 14:19
封装唯一的难点就是子线程和主线程之间的通信
import android.content.Context;import android.os.Bundle;import android.os.Message;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import java.util.Map;public class WebSerivceConnection {    //定义一个Handler主要用于线程通信    private MyHandler myHandler;    //定义一个Message用来封装要传递的数据信息    private Message message;    private  String url,nameSpace,methodName;    private  Map<String,Object> prarms;    private  ResultCallBack resultCallBack;    /**     *构造器     * @param url 接口地址     * @param nameSpace 命名空间     * @param methodName  方法名     *  @param prarms   请求所需的参数     * @param resultCallBack 回调接口的实例     *  */    public  WebSerivceConnection(Context context, String url,                                  String nameSpace,  String methodName,                                  Map<String,Object> prarms,ResultCallBack resultCallBack,int code){        myHandler=new MyHandler(context,code);        message=new Message();        this.resultCallBack=resultCallBack;        this.methodName=methodName;        this.nameSpace=nameSpace;        this.prarms=prarms;        this.url=url;       }    public  void webServiceServer(){         new Thread(){             String result;             @Override             public void run() {                 result= Connection(url, nameSpace, methodName, prarms);                if(result!=null){                    message.what=1;                    message.obj=resultCallBack;                    Bundle bundle = new Bundle();                    bundle.putString("result",result);                    message.setData(bundle);                    myHandler.sendMessage(message);                }else {                    message.what=2;                    myHandler.sendMessage(message);                }             }         }.start();    }    /**     * webservice 请求的方法     * @param url 接口地址     * @param nameSpace 命名空间     * @param methodName  方法名     * @param prarms  请求参数*/    private String Connection(String url, String nameSpace, String methodName, Map<String,Object> prarms){         String data=null;          SoapObject soapObject=new SoapObject(nameSpace,methodName);          //传递参数          for(Map.Entry<String,Object> ele:prarms.entrySet()){              soapObject.addProperty(ele.getKey(),ele.getValue());          }          //创建HttpTransportsSE对象。通过HttpTransportsSE类的构造方法可以指定WebService的WSDL文档的URL          HttpTransportSE httpTransportSE=new HttpTransportSE(url);          httpTransportSE.debug=true;          //生成调用Webservice方法的SOAP请求信息          SoapSerializationEnvelope serializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER10);          serializationEnvelope.bodyOut=soapObject;          serializationEnvelope.dotNet=false;          serializationEnvelope.setOutputSoapObject(soapObject);          //使用call方法调用WebService方法          try {              // 开始网络通信              httpTransportSE.call(null,serializationEnvelope);              SoapObject soapObject1=(SoapObject)serializationEnvelope.bodyIn;              data=soapObject1.getProperty(0).toString();          } catch (Exception e) {              e.printStackTrace();              return  null;          }          return data;      }}
写一个MyHandler 
import android.content.Context;import android.os.Handler;import android.os.Message;import android.widget.Toast;/** * Created by 李腾伟 on 2016/12/9 10:47 * E-mail:tay0929@163.com * 类描述: * 修改人: * 修改时间: * TEL:13592534358 */public class MyHandler extends Handler {    private  Context context;    private int code;    public  MyHandler(Context context,int code){        this.context=context;        this.code=code;    }    @Override    public void handleMessage(Message msg) {        switch (msg.what){            case 1:              ResultCallBack resultCallBack= (ResultCallBack)msg.obj;                resultCallBack.callBack(msg.getData().get("result"),code);            break;            case 2:                Toast.makeText(context,"服务器崩溃了",Toast.LENGTH_LONG).show();            break;        }    }}
然后可以写一个BaseActivity来继承Activity,其他的类可以继承BaseActivity 然后用下面的方法传参就去就能直接请求
public void MyWebServier(String url, String mingMin_name, String fangFa_name,                         Map<String, Object> prarms, ResultCallBack resultCallBacks, int code) {    if (NetworkControl.getNetworkState(this)) {        startDaiod();        WebSerivceConnection webSerivceConnection = new WebSerivceConnection(this, url, mingMin_name, fangFa_name, prarms, resultCallBacks, code);        webSerivceConnection.webServiceServer();    } else {        ToastClarr(getApplicationContext(),"请检查您的网络状态!");        //Toast.makeText(this, "请检查您的网络状态!", Toast.LENGTH_LONG).show();    }}


0 0