Android 调用WebService 查手机号的应用

来源:互联网 发布:德勤财务咨询 知乎 编辑:程序博客网 时间:2024/06/08 09:18

适用网上的WebService 服务
http://www.webxml.com.cn/zh_cn/index.aspx 登录该网站:

WebXml.com.cn 国内手机号码归属地查询WEB服务,提供最新的国内手机号码段归属地数据,每月更新。
使用本站 WEB 服务请注明或链接本站:http://www.webxml.com.cn/ 感谢大家的支持!

具详细如下:

支持下列操作。有关正式定义,请查看服务说明。

getDatabaseInfo

获得国内手机号码归属地数据库信息

输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。

getMobileCodeInfo

获得国内手机号码归属地省份、地区和手机卡类型信息

输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。

SOAP 1.1以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。POST /WebServices/MobileCodeWS.asmx HTTP/1.1Host: webservice.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">      <mobileCode>string</mobileCode>      <userID>string</userID>    </getMobileCodeInfo>  </soap:Body></soap:Envelope>HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>    </getMobileCodeInfoResponse>  </soap:Body></soap:Envelope>SOAP 1.2以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。POST /WebServices/MobileCodeWS.asmx HTTP/1.1Host: webservice.webxml.com.cnContent-Type: application/soap+xml; charset=utf-8Content-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 OKContent-Type: application/soap+xml; charset=utf-8Content-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>HTTP GET以下是 HTTP GET 请求和响应示例。所显示的占位符需替换为实际值。GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string HTTP/1.1Host: webservice.webxml.com.cnHTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><string xmlns="http://WebXml.com.cn/">string</string>HTTP POST以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。POST /WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1Host: webservice.webxml.com.cnContent-Type: application/x-www-form-urlencodedContent-Length: lengthmobileCode=string&userID=stringHTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><string xmlns="http://WebXml.com.cn/">string</string>

1 先建立Android工程

2 监理遵循SOAP1.2协议的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/">      <mobileCode>$mobile</mobileCode>      <userID></userID>    </getMobileCodeInfo>  </soap12:Body></soap12:Envelope>

userID 我们不是填写,因为我们是个人用户,不商业用户
mobileCode我们需要填写占位符,我们用 $mobile 来带。

3编写WebService 的业务类:

package com.wangjialin.internet.service;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import org.xmlpull.v1.XmlPullParser;import android.util.Xml;import com.wangjialin.internet.utils.StreamTool;public class WebServiceRequestFromAndroid {    /**     * 获取手机号归属地     * @param mobile 手机号     * @return     * @throws Exception     */    public static String getAddress(String mobile) throws Exception{        String soap = readSoap();        soap = soap.replaceAll("\\$mobile", mobile);        byte[] entity = soap.getBytes();        String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";        HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();        conn.setConnectTimeout(5000);        conn.setRequestMethod("POST");        conn.setDoOutput(true);        conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");        conn.setRequestProperty("Content-Length", String.valueOf(entity.length));        conn.getOutputStream().write(entity);        if(conn.getResponseCode() == 200){            return parseSOAP(conn.getInputStream());        }        return null;    }    /*     <?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>     */    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;    }    private static String readSoap() throws Exception{        InputStream inStream = WebServiceRequestFromAndroid.class.getClassLoader().getResourceAsStream("AndroidInteractWithWebService.xml");        byte[] data = StreamTool.read(inStream);        return new String(data);    }}

StreamTool的工具类:

package com.wangjialin.internet.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTool {    /**     * 从流中读取数据     * @param inStream     * @return     */    public static byte[] read(InputStream inStream) throws Exception{        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();        byte[] buffer = new byte[1024];        int len = 0;        while( (len = inStream.read(buffer)) != -1){            outputStream.write(buffer, 0, len);        }        inStream.close();        return outputStream.toByteArray();    }}

其他文件布局代码就不做论述了!

0 0