Web Service 查询手机号码归属地
Web Service 是一种基于 SOAP 的远程调用标准,通过它可以将不同操作系统平台,不同语言,不同技术整合到一起;也是一种基于 Web 的服务,它向外部
在 Android 平台调用 Web Service 需要依赖于第三方类库 ksoap2,它是一个 SOAP Web Service 客户端开发包,主要用于资源受限的 Java 环境,如 Applets 或 J2EE 应用程序(CLDC/CDC/MIDP)。在 Android 平台中并不能直接使用 ksoap2,而是使用 ksoap2 Android。ksoap2 Android 是 Android 平台上一个高效、轻量级的 SOAP 开发包,等同于 Android 平台上的 ksoap2 的移植版本。
在 Android 中使用 ksoap2 Android 调用 Web Service 的步骤如下:
1、创建 HttpTransportSE 对象,通过 HttpTransportSE 类的构造方法可以指定 Web Service 的 WSDL(Web Services Description Language) 文档的 URL,该 URL 由 Web Service 提供。
2、创建 SoapSerializationEnvelope 对象,通过构造方法设置 SOAP 的版本号,该版本号需要根据服务端 Web Service 的版本号设置。在创建 SoapSerializationEnvelope 对象后,还需要设置 SoapSerializationEnvelope 类的 bodyOut 属性。
3、创建 SoapObject 对象,该类构造方法的第一个参数表示 Web Service 的命名空间,可以从 WSDL 文档中找到,第二个参数表示要调用的 Web Service 方法名。
4、如果需要给 Web Service 传递参数,需要使用 SoapObject 类的 addProperty(String key, Object value) 方法,其中 key 是 Web Service 要求的参数。
5、调用 SoapSerializationEnvelope 类的 setOutputSoapObject() 方法或者直接使用 bodyOut 属性,将 SoapObject 对象附加给 SoapSerializationEnvelope,以明确请求参数。
6、调用 HttpTransportSE 的 call 方法,执行 Web Service 访问。call 方法有两个参数,第一个是 Web Service 的 SOAPAction,第二个是SoapSerializationEnvelope 对象。
7、获取返回的参数,通过 SoapSerializationEnvelope 对象的 bodyIn 属性可以获取 SoapObject 对象,该对象是 Web Service 返回的信息,解析该对象就能得到返回值。

 
1 import java.io.IOException; 2 3 import org.ksoap2.SoapEnvelope; 4 import org.ksoap2.serialization.SoapObject; 5 import org.ksoap2.serialization.SoapSerializationEnvelope; 6 import org.ksoap2.transport.HttpTransportSE; 7 import org.xmlpull.v1.XmlPullParserException; 8 9 import android.os.Bundle; 10 import android.os.Handler; 11 import android.os.Message; 12 import android.app.Activity; 13 import android.util.Log; 14 import android.view.Menu; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.widget.Button; 18 import android.widget.EditText; 19 import android.widget.TextView; 20 21 public class MainActivity extends Activity { 22 23 EditText et; 24 Button bt; 25 TextView tv; 26 27 Handler handler = new Handler(){ 28 @Override 29 public void handleMessage(Message msg) { 30 super.handleMessage(msg); 31 32 tv.append(msg.obj.toString()+"\n"); 33 } 34 }; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 40 setContentView(R.layout.activity_main); 41 et = (EditText) findViewById(R.id.et); 42 tv = (TextView) findViewById(R.id.tv); 43 bt = (Button) findViewById(R.id.bt); 44 bt.setOnClickListener(new OnClickListener(){ 45 @Override 46 public void onClick(View v) { 47 // 验证手机号码的合法性 48 String p = et.getText().toString().trim(); 49 Log.i("Tag", p); 50 //if(p.length()<11) return; 51 getPhoneWebService(p); 52 }} 53 ); 54 } 55 56 // 调用 Web Services 查看手机所属地 57 public void getPhoneWebService(final String phone) { 58 59 new Thread(new Runnable() { 60 61 @Override 62 public void run() { 63 // 以下信息由 Web Service 提供 64 //String surl = "http://webservice.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx"; 65 String surl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; 66 String ns = "http://WebXml.com.cn/"; 67 //String method = "getAddressByZipCode"; 68 String method = "getMobileCodeInfo"; 69 //final String soapAction = "http://WebXml.com.cn/getAddressByZipCode"; 70 final String soapAction = "http://WebXml.com.cn/getMobileCodeInfo"; 71 // 1、创建 HttpTransportSE,用于调用 Web Service 72 final HttpTransportSE htse = new HttpTransportSE(surl); 73 // 2、生成调用 Web Service 方法的 SOAP 请求信息,并指定 SOAP 的版本 74 final SoapSerializationEnvelope sse = new SoapSerializationEnvelope( 75 SoapEnvelope.VER11); 76 sse.dotNet = true; 77 // 3、创建 SoapObject,可以传进参数,以下两个参数是 Web Service 要求的 78 SoapObject so = new SoapObject(ns, method); 79 //so.addProperty("theZipCode", phone); 80 so.addProperty("mobileCode", phone); 81 so.addProperty("userID", ""); 82 Log.i("Tag", so.toString()); 83 // 4、设置 SOAP 的传出消息体 84 sse.bodyOut = so; 85 // 5、调用 Web Service 开启线程 86 try { 87 // 执行调用 88 htse.call(soapAction, sse); 89 // 6、处理返回结果 90 String result; 91 if (sse.getResponse() != null) { 92 // 获取返回的数据 93 SoapObject object = (SoapObject) sse.bodyIn; 94 Log.i("Tag", object.toString()); 95 // 获取返回的结果 96 result = object.getProperty(0).toString(); 97 } else { 98 result = "WebService 调用无返回值"; 99 } 100 Message msg = new Message(); 101 msg.obj = result; 102 handler.sendMessage(msg); 103 } catch (IOException e) { 104 // TODO Auto-generated catch block 105 e.printStackTrace(); 106 } catch (XmlPullParserException e) { 107 // TODO Auto-generated catch block 108 e.printStackTrace(); 109 } 110 } 111 }).start(); 112 } 113 }
布局文件
 
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 <LinearLayout 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" 13 android:orientation="horizontal" 14 android:id="@+id/layout" 15 > 16 <TextView 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="手机号码:"/> 20 21 <EditText 22 android:id="@+id/et" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_weight="1.0" 26 android:inputType="number" /> 27 28 <Button 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="查询" 32 android:id="@+id/bt" 33 /> 34 </LinearLayout> 35 <TextView 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:layout_below="@id/layout" 39 android:id="@+id/tv"/> 40 41 </RelativeLayout>
添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
 
                    
                
 
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号