异步调用webservice

mingming 2013-06-24

参考:http://blog.csdn.net/lyq8479/article/details/6428288

增加网络权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.ADemo10"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="15"/>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
    <uses-permission android:name="android.permission.INSTALL_PACKAGES"></uses-permission>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                  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>
package com.example.ADemo10;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.Date;

/**
 * 最好使用异步调用,否则可能产生UI阻塞错误。
 * 记得增加网络权限
 * myTextView.setText(result); 虽然这里能设定结果,
 * 但是设定这里的时候,出现异常,还不知道原因
 */
public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    private Button button;
    private EditText editText;
    private TextView textView2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.button);
        editText = (EditText) findViewById(R.id.editText);
        textView2 = (TextView) findViewById(R.id.textView2);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!validate()) {
                    textView2.setText((new Date()).toString() + ":请输入信息...");
                } else {
                    textView2.setText((new Date()).toString() + ":开始查询.....");
                    AnsyTry anys = new AnsyTry(textView2);
                    anys.execute();
                }

            }
        });

    }

    public boolean validate() {
        String str = editText.getText().toString();
        if (str == null || str.trim().length() == 0) {
            return false;
        }

        return true;
    }

    public void searchFromWebService(TextView myTextView) {
        String nameSpace = "http://WebXml.com.cn/";
        String methodName = "getMobileCodeInfo";
        String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
        String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";


        try {
            SoapObject rpc = new SoapObject(nameSpace, methodName);
            rpc.addProperty("mobileCode", editText.getText().toString());
            rpc.addProperty("userId", "");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
            envelope.bodyOut = rpc;
            envelope.dotNet = true;
            envelope.setOutputSoapObject(rpc);
            HttpTransportSE transport = new HttpTransportSE(endPoint);
            transport.call(soapAction, envelope);
            SoapObject object = (SoapObject) envelope.bodyIn;
            String result = object.getProperty(0).toString();
            myTextView.setText(result);
        } catch (Exception e) {
            if (e.getStackTrace() != null) {
                for (Object o : e.getStackTrace()) {
                    myTextView.setText(myTextView.getText() + "\n" + o.toString());
                }
            }
        }
    }
    //异步处理程序
    class AnsyTry extends AsyncTask<String, TextView, Double> {

        TextView te = null;

        public AnsyTry(TextView te) {
            super();
            this.te = te;
        }

        @Override
        protected Double doInBackground(String... params) {
            double dou = 0;
            dou = 100;
            searchFromWebService(te);
            publishProgress(te);
            return dou;
        }

        @Override
        protected void onPostExecute(Double result) {
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(TextView... values) {
            super.onProgressUpdate(values);
        }

    }

}

相关推荐