美文网首页程序员
soap数据请求

soap数据请求

作者: Mircale1 | 来源:发表于2019-05-06 19:07 被阅读7次

    packagecom.example.jack.travlelikerwebservices;

    importandroid.app.Activity;

    importandroid.os.Handler;

    importandroid.os.Bundle;

    importandroid.os.Message;

    importandroid.util.Log;

    importandroid.widget.Toast;

    importorg.ksoap2.SoapEnvelope;

    importorg.ksoap2.serialization.SoapObject;

    importorg.ksoap2.serialization.SoapSerializationEnvelope;

    importorg.ksoap2.transport.HttpTransportSE;

    importorg.xmlpull.v1.XmlPullParserException;

    importjava.io.IOException;

    importjava.util.List;

    importjava.util.Map;

    public classMainActivityextendsActivity {

    privateStringresult;

    Handlerhandler=newHandler(){

    @Override

    public voidhandleMessage(Message msg) {

    super.handleMessage(msg);

    switch(msg.what){

    case0:

    Toast.makeText(MainActivity.this,"连接服务器失败", Toast.LENGTH_LONG).show();

    case1:

    Toast.makeText(MainActivity.this,result, Toast.LENGTH_LONG).show();

    break;

    }

    }

    };

    @Override

    protected voidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    btnclick();

    }

    public voidbtnclick(){

    finalString SERVICE_NS ="http://tempuri.org/";//接口里面serveice_ns 就是命名空间

    finalString SOAP_ACTION ="http://tempuri.org/PCCSearch ";// 接口中soap_action  方法名

    finalString SERVICE_URL ="http://develop.apis.travelconnect.co/TravelAgentAPI.asmx";//URL地址,这里写发布的网站的本地地址

    Log.i("====","==1==");

    String methodName ="PCCSearch ";//xml中包含数据的变量名

    //创建HttpTransportSE传输对象,该对象用于调用Web Service操作

    finalHttpTransportSE ht =newHttpTransportSE(SERVICE_URL);

    ht.debug=true;//是否允许调试

    //使用SOAP1.1协议创建Envelop对象。从名称上来看,SoapSerializationEnvelope代表一个SOAP消息封包;但ksoap2-android项目对

    //SoapSerializationEnvelope的处理比较特殊,它是HttpTransportSE调用Web Service时信息的载体

    finalSoapSerializationEnvelope envelope =newSoapSerializationEnvelope(SoapEnvelope.VER11);

    //类似与开始调用数据前赋予你的数据放在哪里 然后调用的数据的变变量名是什么

    SoapObject soapObject =newSoapObject(SERVICE_NS, methodName);

    soapObject.addProperty("pcc","73aw");      //进行传入post方法需要的参数

    soapObject.addProperty("count","1");

    soapObject.addProperty("lang","zh_HK");

    //设置soap 对.net写的接口的支持

    envelope.dotNet=true;

    //类似与 把客户端需要传入数据的赋值  全都放在 bodyout 中然后通过bodyin 进行读取数据

    envelope.bodyOut= soapObject;

    Log.i("====","==3==");

    //soap解析数据时需要使用到子线程来辅助soap进行解析

    newThread(){

    @Override

    public voidrun() {

    try{

    //调用WebService,调用对象的call()方法,并以SoapSerializationEnvelope作为参数调用远程Web Service

    ht.call(SOAP_ACTION,envelope);

    if(envelope.getResponse() !=null){

    //进行获取数据

    SoapObject so = (SoapObject)envelope.bodyIn;

    /*result = so.getPropertyAsString(0); //赋值*/

    for(inti=0;i

    Log.v("====","==4=="+ so.getProperty(i)); //拿到数据后进行解析

    }

    //接下来就是从SoapObject对象中解析响应数据的过程了

    Message msg =newMessage();

    msg.what=1;

    handler.sendMessage(msg);//进行对服务器是否相应的判断

    }

    else{

    Message msg=newMessage();

    msg.what=0;

    handler.sendMessage(msg);

    }

    }catch(IOException e) {

    e.printStackTrace();

    }catch(XmlPullParserException e) {

    e.printStackTrace();

    }

    }

    }.start();

    }

    }

    相关文章

      网友评论

        本文标题:soap数据请求

        本文链接:https://www.haomeiwen.com/subject/trppittx.html