异步调用webservice
参考:http://blog.csdn.net/lyq8479/article/details/6428288
增加网络权限
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Xml代码 <?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> |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
Java代码 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); } } } |
