zoukankan      html  css  js  c++  java
  • Android调用WebService

     最近开始物联网方面的学习,第一次接触Android开发,学习中遇到了需要调用PC端WebService的问题,现在将自己学习到的知识记录下来,以便今后复习。

     准备工作:

            1下载第三方类库Ksoap2。

            2.使用Eclipse新建Android项目并将下载过来的Ksoa2复制进Android项目lib文件夹下。

            3.使用VS2012新建一个WebService并发布在IIS上

      调用WebService具体步骤:

            1.创建一个SoapObject对象

    SoapObject soapObject = new SoapObject(namepace, methodName);

       namepace为命名空间,methodName为要调用的方法

       2.设置要调用方法的参数,若果方法没有参数可不填

    soapObject.addProperty(“param1”,”value”);
    soapObject.addProperty(“param2”,”value”);

       param为参数名,value为参数值

       3.创建SoapSerializationEnvelope对象并将第一步创建的soapobject赋值给boduOut属性

    SoapSerializationEnvelope sse= new SoapSerializationEnvelope(SoapEnvelope.VER10);
    sse.bodyOut = soapObject ;

       SoapEnvelope.VER10为SOAP的版本号,根据服务端的版本号设置

       4.创建HttpTransportSE对象

    HttpTransportSE htse= new HttpTransportSE(URL);

        URL为WebService的WSDL文档地址

       5.调用Call方法获得返回值  

    htse.call(namepace + methodName, sse)

       call的第一个参数为命名空间+方法名的String值,第二个参数为第三步创建的SoapSerializationEnvelope

       6.保存返回值

    SoapObject soapResponse = (SoapObject) sse.bodyIn;
    String result = soapResponse.getProperty(0).toString();

       使用SoapSerializationEnvelope类的bodyIn属性得到返回的SoapObject,并赋值给String。

      注意点:1.在Android4.0后访问网络的方法不能再UI线程中执行,需新建一个Thred

          2.在AndroidManifest文件夹添加访问网络的权限

            <uses-permission android:name="android.permission.INTERNET" /> 

          3.子线程禁止更新UI,所以若想将WebService中返回的数据显示在界面上需要使用Handle和Message

        接下来将演示调用新建的WebService自带的HelloWrod方法。

    public class MainActivity extends Activity {
        private EditText et_URL;
        private SoapObject soap;
        private SoapObject soapResponse;
    
        private String namepace = "http://tempuri.org/";
        private String methodName = "HelloWorld";
        private String URL;
        private SoapSerializationEnvelope sse;
        private String result;
        private Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                ((TextView) findViewById(R.id.textView1)).setText((String) msg.obj);
            };
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
        public void initView() {
            
            findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    et_URL = (EditText) findViewById(R.id.et_URL);
                    URL = et_URL.getText().toString();
                    new Thread(){
                        public void run() {
                            toService();
                        };
                    }.start();
                }
            });
    
        }
        public void toService() {
            soap = new SoapObject(namepace, methodName);
            sse = new SoapSerializationEnvelope(SoapEnvelope.VER10);
            sse.bodyOut = soap;
            sse.dotNet = true;
            HttpTransportSE htse = new HttpTransportSE(URL);
            try {
                htse.call(namepace + methodName, sse);
                soapResponse = (SoapObject) sse.bodyIn;
                result = soapResponse.getProperty(0).toString();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
            Message msg = new Message();
            msg.obj = result;
            handler.sendMessage(msg);
        }
    }
  • 相关阅读:
    [模板] 循环数组的最大子段和
    [最短路][几何][牛客] [国庆集训派对1]-L-New Game
    [洛谷] P1866 编号
    1115 Counting Nodes in a BST (30 分)
    1106 Lowest Price in Supply Chain (25 分)
    1094 The Largest Generation (25 分)
    1090 Highest Price in Supply Chain (25 分)
    树的遍历
    1086 Tree Traversals Again (25 分)
    1079 Total Sales of Supply Chain (25 分 树
  • 原文地址:https://www.cnblogs.com/LandMine/p/4452907.html
Copyright © 2011-2022 走看看