android调用 .net webService

 

package com.rockcheck.mes;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.HttpTransportSE;


public class PDAActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pda_home);
    }

    //点击按钮事件调用异步
    public void webQuery (View view){
        new DownloadTask().execute();
    }

    //
    public Object web (){
        Object result=null;
        String asmx_url="http://xxx/Service1.asmx";
        String nameSpace="http://tempuri.org/";
        String methodName="xxx";

        //指定webservice的命名空间和调用方法
        SoapObject soapObject = new SoapObject(nameSpace,methodName);
        //设置需要调用的webServices接口需要传入的参数,参数1 string,参数2 object
        soapObject.addProperty("aaa",null);
        //调用webService方法的SOAP请求信息,并指定SOAP的版本 ,这个版本 根据 asdl文件中是1.1还是1.2协议来指定的. 一般VER11或者VER12都可以,以前还有VER10
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
        //创建的SoapObject对象设为SoapSerializationEnvelope的传出SOAP消息体
        envelope.bodyOut = soapObject;
        //设置是否调用的是dotNet开发的webService
        envelope.dotNet = true;
        //创建HttpTransportSE对象,该对象用于调用webService操作
        HttpTransportSE transportSE = new HttpTransportSE(asmx_url);
        //使用调试功能
        transportSE.debug = true;
       try {
           //调用操作对象call方法,将SoapSerializationEnvelope作为参数调用远程webService
           transportSE.call(nameSpace+methodName,envelope);
           result = (Object) envelope.bodyIn;
           //result = (Object) envelope.getResponse();
        } catch (Exception e) {
           e.printStackTrace();
        }
       //Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();
        return result;
    }

    //
    class DownloadTask extends AsyncTask<Void,Integer,Boolean> {
        Object  result=null;
        @Override
        protected Boolean doInBackground(Void... voids) {
            result =  web(); //在子线程中请求webservice
            return null;
        }
        @Override
        protected void onPostExecute(Boolean aBoolean) {
            Toast.makeText(PDAActivity.this, result.toString(), Toast.LENGTH_SHORT).show();
        }
    }
}

 

posted @ 2018-04-13 21:43  富坚老贼  阅读(137)  评论(0编辑  收藏  举报