java/android查询手机固话归属地、GSM卡信息

来源:互联网 发布:mac os sierra正式推送 编辑:程序博客网 时间:2024/05/16 16:14

Model.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>

PhoneNumberInfo.java

package com.zontin.android.util;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import org.xmlpull.v1.XmlPullParser;import android.util.Xml;public class PhoneNumberInfo {public static String findAddress(String mobile) throws Exception {        InputStream is = PhoneNumberInfo.class.getClassLoader()                .getResourceAsStream("Model.xml");        byte[] data = StreamTool.readStream(is);        String xml = new String(data, "UTF-8");        String soap = xml.replaceAll("\\$mobile", mobile);        byte[] result = soap.getBytes("UTF-8");        String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";        URL url = new URL(path);        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        conn.setRequestMethod("POST");        conn.setDoOutput(true);        conn.setConnectTimeout(5 * 1000);         conn.setRequestProperty("Content-Type",                "application/soap+xml; charset=utf-8");        conn.setRequestProperty("Content-Length", String.valueOf(result.length));        OutputStream os = conn.getOutputStream();        os.write(result);        os.flush();        os.close();         InputStream isSocp = conn.getInputStream();        return parse(isSocp);    }     public static String parse(InputStream is) throws Exception {        XmlPullParser parser = Xml.newPullParser();        parser.setInput(is, "UTF-8");        int event = parser.getEventType();        while (event != XmlPullParser.END_DOCUMENT) {            switch (event) {            case XmlPullParser.START_TAG:                if ("getMobileCodeInfoResult".equals(parser.getName())) {                    return parser.nextText();                }                break;            }            event = parser.next();        }        return null;    }}

StreamTool.java

package com.zontin.android.util;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTool {/**     * 读取输入流数据     *     * @param is     * @return     * @throws Exception     */     public static byte[] readStream(InputStream is) throws Exception {        ByteArrayOutputStream os = new ByteArrayOutputStream();        byte[] buffer = new byte[2048];        int len = 0;        while ((len = is.read(buffer)) != -1) {            os.write(buffer, 0, len);        }        is.close();        return os.toByteArray();    }}

以上准备好现在就可以调用了:

try {        Log.d("QY", "XXX号码的归属地信息:"+PhoneNumberInfo.findAddress( "这里传入号码"));        } catch (Exception e) {            Log.d("QY", "查询失败"+e.toString());        }


原创粉丝点击