android 手机号码归属地查询

package com.cy.contact.net;

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

import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.util.Xml;

/** 
 * @author 作者wangqiang
 * @version 创建时间:2012-12-29 下午04:36:51 
 * 
 */
public class DialHttp {

	
	public String query(String num,Context context) throws Exception { 
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("number_address.xml"); 
        byte[] data = load(in); 
        String xml = new String(data); 
        xml = xml.replace("#", num); 
        byte[] sendData = xml.getBytes("UTF-8"); 
        // 发送到代理的地址上 
        URL url = new URL( "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
        conn.setRequestMethod("POST"); 
        conn.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8"); 
        conn.setRequestProperty("Content-Length", String.valueOf(sendData.length)); 
        // 将请求的xml发送出去 
        conn.setDoOutput(true); 
        conn.getOutputStream().write(sendData); 
        // 获取从服务器传回来的数据 
        if (conn.getResponseCode() == 200) 
        {
        	return parse(conn.getInputStream());
        	}
        return null; 
    } 
  
    // 解析流拿到getMobileCodeInfoResult中的数据 
    private String parse(InputStream inputStream) throws Exception { 
        XmlPullParser parser = Xml.newPullParser(); 
        parser.setInput(inputStream, "UTF-8"); 
        // 查找getMobileCodeInfoResult标签,获取标签中的数据 
        for (int event = parser.getEventType(); event != XmlPullParser.END_DOCUMENT; event = parser 
                .next()) 
            switch (event) { 
            case XmlPullParser.START_TAG: 
                if ("getMobileCodeInfoResult".equals(parser.getName())) 
                    return parser.nextText(); 
            } 
        return null; 
    } 
    
    public byte[] load(InputStream in) throws IOException{
    	byte b[] = new byte[1024];
    	int len = 0;
    	int temp = 0;
    	while((temp = in.read()) != -1){
    		b[len] = (byte)temp;
    		len++;
    	}
		in.close();
		return b;
    }    
    
    
}
 
 
src下放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>#</mobileCode> 
            <userID></userID> 
        </getMobileCodeInfo> 
    </soap12:Body> 
</soap12:Envelope>

 
 
得到数据格式  省+城市
	VerNumeral ver = new VerNumeral();
		String number = "13576127225:江西 南昌 江西移动全球通卡";
		String str[] = number.split(":");
		
		
		String[] m = str[1].split(" ");
		String result = m[0]+m[1];
		System.out.println("m[2]--"+m[2]);
		System.out.println("s--"+result);

 

你可能感兴趣的:(android 手机号码归属地查询)