Android调用Java WebSevice篇之二

1.创建Activity。

package com.web;



import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;



import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.os.StrictMode;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;



public class WebServiceTestActivity extends Activity implements OnClickListener {



    private EditText et_param01;

    private EditText et_param02;

    private EditText et_param03;

    private Button btn_submit;



    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);



        et_param01 = (EditText) findViewById(R.id.et_param01);

        et_param02 = (EditText) findViewById(R.id.et_param02);

        et_param03 = (EditText) findViewById(R.id.et_param03);

        btn_submit = (Button) findViewById(R.id.btn_submit);

        btn_submit.setOnClickListener(this);



        /**

         * 防止android.os.NetworkOnMainThreadException异常 

         * 参考http://dev.wo.com.cn/docportal/doc_queryMdocDetail.action?mdoc.docindex=6695

         */

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()

                .detectDiskReads().detectDiskWrites().detectNetwork()

                .penaltyLog().build());

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()

                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()

                .penaltyLog().penaltyDeath().build());



    }



    

    public String callService(String param1, String param2) {

        try {

            // 命名空间

            String nameSpace = "http://10.8.60.63:8088/webservice/services/webCallService/";

            // 调用的方法名称

            String methodName = "callService";

            // SOAP Action

            String soapAction = nameSpace + methodName;

            SoapObject request = new SoapObject(nameSpace, methodName);

            request.addProperty("param1", param1);

            request.addProperty("param2", param2);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

                    SoapEnvelope.VER11);

            envelope.dotNet = true;

            envelope.bodyOut = request;

            envelope.setOutputSoapObject(request);

            envelope.encodingStyle="UTF-8";

            HttpTransportSE transport = new HttpTransportSE(

                    "http://10.8.60.63:8088/webservice/services/webCallService");// wsdl文档



            try {

                // 调用WebService

                transport.call(soapAction, envelope);

                // transport.call(null, envelope);

            } catch (Exception e) {

                e.printStackTrace();

            }

            Object obj =  envelope.getResponse();

            

            return obj.toString();

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            return null;

        }



    }



    @Override

    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.btn_submit:

            new Thread() {

                @Override

                public void run() {

                    // 你要执行的方法

                    // 执行完毕后给handler发送一个空消息

                    handler.sendEmptyMessage(0);

                }

            }.start();

            break;

        }

    }



    // 定义Handler对象

    private Handler handler = new Handler() {

        @Override

        // 当有消息发送出来的时候就执行Handler的这个方法

        public void handleMessage(Message msg) {

            // 处理UI

            String str = (String) callService(

                    et_param01.getText().toString(), et_param02.getText()

                            .toString());

            if (str == null || "".equals(str)) {

                str = "返回值为空";

            }

            et_param03.setText(str);



        }

    };



}
View Code

2.配置清单文件。

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.web"

    android:versionCode="1"

    android:versionName="1.0" >



    <uses-sdk

        android:minSdkVersion="9"

        android:targetSdkVersion="15" />

    <!-- 访问网络的权限 -->

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

    

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" >

        <activity

            android:name=".WebServiceTestActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />



                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

     

    

</manifest>
View Code

3.测试效果。

Android调用Java WebSevice篇之二

你可能感兴趣的:(Java Web)