我的Android进阶之旅------>Android通过调用Webservice实现手机号码归属地查询

简介: 此app的实现功能如图所示:   注:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx是本文webservice的提供商 具体的用法见:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo   以下是 SOAP 1.2 请求和响应示例。

此app的实现功能如图所示:

 

注:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx是本文webservice的提供商

具体的用法见:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo

 

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

 

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap12:Body>
</soap12:Envelope>

 

 

step1:创建android工程MobileAddressQuery,并设置网络访问权限。



step2:设计应用的UI界面和使用的字符串值

/res/values/string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">手机号码归属地查询</string>
    <string name="mobile">手机号码</string>
    <string name="button">归属地查询</string>
    <string name="error">查询失败</string>
</resources>


 


布局 /res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/mobile" />
	<EditText android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/mobile" />
	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/button"
		android:onClick="query" /><!-- 指定button的响应方法为query -->
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/textView" />
</LinearLayout>


step3:在src目录下创建sop12.xml,并将网址文档中提供的代码复制其中,如下

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
    <!-- $mobile只是展示如此,在程序调用此xml文件后会将用户输入的手机号码代替 -->
      <mobileCode>$mobile</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

step4:业务类:cn.roco.address.AddressService

注意访问目标地址是:

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx

可以有协议中得到。

package cn.roco.address.service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import cn.roco.utils.StreamTools;

public class AddressService {
	/**
	 * 用于获取手机号归属地  调用webservice
	 * @param mobile  手机号
	 * @return
	 * @throws Exception
	 */
	public static String getAddress(String mobile) throws Exception {
		String soap = readSoap();
		/**
		 * 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile  
		 *  而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;  
		 */
		soap = soap.replaceAll("\\$mobile", mobile); //将用户输入的手机号码代替sop12.xml中的$mobile
		byte[] entity = soap.getBytes();
		
		String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  //webservice提供源
		HttpURLConnection connection = (HttpURLConnection) new URL(path)
				.openConnection();
		connection.setConnectTimeout(5000);
		connection.setRequestMethod("POST");
		connection.setRequestProperty("Content-Type",
				"application/soap+xml; charset=utf-8");
		connection.setRequestProperty("Content-Length",
				String.valueOf(entity.length));
		connection.setDoOutput(true);
		connection.getOutputStream().write(entity);
		if (connection.getResponseCode() == 200) {
			return parseSOAP(connection.getInputStream());
		}
		return null;
	}
	/**
	 * 解析webservice返回的数据
	 * <?xml version="1.0" encoding="utf-8"?>
		<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
		  <soap12:Body>
		    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
		      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
		    </getMobileCodeInfoResponse>
		  </soap12:Body>
		</soap12:Envelope>
	 * @param inputStream
	 * @return 
	 * @throws Exception 
	 */
	private static String parseSOAP(InputStream xml) throws Exception {
		XmlPullParser pullParser=Xml.newPullParser();
		pullParser.setInput(xml,"UTF-8");
		int event=pullParser.getEventType();
		while(event!=XmlPullParser.END_DOCUMENT){
			switch (event) {
			case XmlPullParser.START_TAG:
				if ("getMobileCodeInfoResult".equals(pullParser.getName())) {
					return pullParser.nextText();
				}
				break;
			}
			event=pullParser.next();
		}
		return null;
	}
	/**
	 * 读取sop12.xml的内容
	 * @return
	 * @throws Exception
	 */
	private static String readSoap() throws Exception {
		InputStream inputStream = AddressService.class.getClassLoader()
				.getResourceAsStream("sop12.xml");
		byte[] data = StreamTools.read(inputStream);
		return new String(data);
	}
}


 

 

step5:工具类cn.roco.utils.StreamTool

package cn.roco.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTools {
	/**
	 * 读取输入流中的数据
	 * @param inputStream  输入流
	 * @return   二进制的流数据
	 * @throws Exception
	 */
	public static byte[] read(InputStream inputStream) throws Exception {
		ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
		byte[] buffer=new byte[1024];
		int length=0;
		while((length=inputStream.read(buffer))!=-1){
			outputStream.write(buffer,0,length);
		}
		inputStream.close();
		return outputStream.toByteArray();
	}

}

 

step6:cn.roco.address.MainActivity

package cn.roco.address;

import cn.roco.address.service.AddressService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText mobileText;
	private TextView textView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mobileText = (EditText) this.findViewById(R.id.mobile);
		textView = (TextView) this.findViewById(R.id.textView);
	}

	public void query(View v) {
		String mobile=mobileText.getText().toString();
		try {
			String address=AddressService.getAddress(mobile);
			textView.setText(address);
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), R.string.error, 1).show();
		}
		
	}
}


step7:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="cn.roco.address" android:versionCode="1" android:versionName="1.0">
	<uses-sdk android:minSdkVersion="8" />
	 <!-- 访问Internet权限 -->
	<uses-permission android:name="android.permission.INTERNET"/>
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".MainActivity" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<uses-library android:name="android.test.runner" />
	</application>
	<instrumentation android:targetPackage="cn.roco.address"
		android:name="android.test.InstrumentationTestRunner" />
</manifest>



==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================


 

相关文章
|
API
查手机号归属地免费API接口教程
此API用于查询指定手机号码的归属地信息,包括号段、省份、城市、运营商等。支持POST和GET请求方式,需提供用户ID、KEY及手机号作为参数。返回结果包含状态码、信息提示及详细归属地信息。示例请求地址:https://cn.apihz.cn/api/ip/shouji.php?id=88888888&key=88888888&phone=13219931963。
2278 5
|
开发工具 Android开发
Android平台GB28181设备接入端预置位查询(PresetQuery)探讨和技术实现
之前blog介绍了GB28181云台控制(PTZCmd)相关,本文主要是介绍下GB28181预置位查询。
380 0
|
数据可视化 Java 数据挖掘
手机号归属地查询实例
手机号归属地查询实例
470 0
|
C# 索引
C# | 【完全开源】手机号码归属地查询,一秒内百万次查询
这个开源项目是一个.NET库,可以通过手机号码获取号码归属地信息,包括运营商、国家、省份、城市、邮政编码、区号等信息。 该库加载了一个包含46万条数据的“中国手机号归属地信息”数据集,并实现了高速查询。在我的7年老笔记本上执行一百万次查询耗时不足一秒。
868 0
|
API 网络安全
手机号码归属地可以应用在哪些地方呢?
在手机号码整个使用群体中个人占比高达90%,使用人数大、占比高的特点也造成了电话诈骗的高频发生。同时由于个人信息的泄露,诈骗分子在充分了解了受害人的资料,使诈骗犯罪活动更高的犯罪成功率。在诈骗高发之时,手机号码归属地作为一个通讯衍生的工具,可以第一时间发挥其作用,在接到不明来电时可以通过手机号码归属地起到判断来电的作用。
713 1
手机号码归属地可以应用在哪些地方呢?
|
JavaScript 前端开发 搜索推荐
HTML+JS 实现手机号码归属地查询功能
HTML+JS 实现手机号码归属地查询功能
595 0
HTML+JS 实现手机号码归属地查询功能
|
安全 Java API
手机号码归属地 API 实现防止骚扰电话,看这一篇就够了(内附设计思路和代码)
本文将会深入探讨如何利用手机号码归属地 API 在防止电话骚扰,此外,还会给大家列出手机号码归属地 API 的其他应用场景。
955 0
手机号码归属地 API 实现防止骚扰电话,看这一篇就够了(内附设计思路和代码)
|
API 定位技术 Python
别被骗了,通过手机号码归属地轻松辨别诈骗电话
通过手机号码归属地,我们可以大致了解一个人的所在地,这对于很多人来说是很有用的。
1425 0
别被骗了,通过手机号码归属地轻松辨别诈骗电话
|
Android开发
王者荣耀安卓区修改荣耀战区方法 | 最低战力查询(附带视频与安装包)
王者荣耀安卓区修改荣耀战区方法 | 最低战力查询(附带视频与安装包)
1142 0
用免费的webservice查询手机号
用免费的webservice查询手机号
210 0
用免费的webservice查询手机号

热门文章

最新文章