加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Android下达成电话号码归属地的查询

发布时间:2021-11-25 18:13:06 所属栏目:PHP教程 来源:互联网
导读:需要使用到WebService的服务,这里选择www.webxml.com.cn提供的服务来查询电话号码归属地,使用方法网页上有介绍,这里使用一个实例来演示如何在Android下实现电话号码归属地的查询: 0.使用webxml的soap方式:mobilesoap.xml在src目录下 ?xml version=1.0 e

需要使用到WebService的服务,这里选择www.webxml.com.cn提供的服务来查询电话号码归属地,使用方法网页上有介绍,这里使用一个实例来演示如何在Android下实现电话号码归属地的查询:
 
0.使用webxml的soap方式:mobilesoap.xml在src目录下
 
<?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>  
1.MainActivity.java
 
public class MainActivity extends Activity   
{  
    private EditText mobileText;  
    private TextView addressView;  
    private static final String TAG = "MainActivity";  
      
    @Override  
    public void onCreate(Bundle savedInstanceState)   
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        mobileText = (EditText)this.findViewById(R.id.mobile);  
        addressView = (TextView)this.findViewById(R.id.address);  
        Button button = (Button)this.findViewById(R.id.button);  
        button.setOnClickListener(new View.OnClickListener()   
        {             
            @Override  
            public void onClick(View v)   
            {  
                String mobile = mobileText.getText().toString();  
                InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");  
                try   
                {  
                    addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile));  
                }   
                catch (Exception e)  
                {  
                    Log.e(TAG, e.toString());  
                    Toast.makeText(MainActivity.this, "查询失败", 1).show();  
                }  
            }  
        });  
    }  
}  
2.MobileInfoService.java
 
public class MobileInfoService   
{  
    /**
     * 获得电话号码归属地信息
     * @param inStream
     * @param mobile
     * @return
     * @throws Exception
     */  
    public static String getMobileAddress(InputStream inStream, String mobile)throws Exception  
    {  
        //定义输入流   
        String soap = readSoapFile(inStream, mobile);  
        byte[] data = soap.getBytes();  
        URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");  
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        conn.setRequestMethod("POST");  
        conn.setConnectTimeout(5 * 1000);  
        //如果通过post提交数据,必须设置允许对外输出数据   
        conn.setDoOutput(true);  
        conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");  
        conn.setRequestProperty("Content-Length", String.valueOf(data.length));  
        OutputStream outStream = conn.getOutputStream();  
        outStream.write(data);  
        outStream.flush();  
        outStream.close();  
        if(conn.getResponseCode()==200)  
        {  
            return parseResponseXML(conn.getInputStream());  
        }  
        return null;  
    }  
      
    /**
     * 读取Soap文件
     * @param inStream
     * @param mobile
     * @return
     * @throws Exception
     */  
    private static String readSoapFile(InputStream inStream, String mobile) throws Exception  
    {  
        //读取输入流   
        byte[] data = StreamTool.readInputStream(inStream);  
        String soapxml = new String(data);  
        Map<String, String> params = new HashMap<String, String>();  
        params.put("mobile", mobile);  
        return replace(soapxml, params);  
    }  
    /**
     * 替换占位符方法
     * @param xml
     * @param params
     * @return
     * @throws Exception
     */  
    public static String replace(String xml, Map<String, String> params)throws Exception  
    {  
        String result = xml;  
        if(params!=null && !params.isEmpty())  
        {  
            //循环替换掉所有占位符   
            for(Map.Entry<String, String> entry : params.entrySet())  
            {  
                //需要对$进行转义   
                String name = "//$"+ entry.getKey();  
                //使用正则表达式替换   
                Pattern pattern = Pattern.compile(name);  
                Matcher matcher = pattern.matcher(result);  
                if(matcher.find())  
                {  
                    result = matcher.replaceAll(entry.getValue());  
                }  
            }  
        }  
        return result;  
    }  
      
    /**
     * 解析返回的XML字符串数据
     * @param inStream
     * @return
     * @throws Exception
     */  
    private static String parseResponseXML(InputStream inStream) throws Exception  
    {  
        XmlPullParser parser = Xml.newPullParser();  
        parser.setInput(inStream, "UTF-8");  
        //产生第一个事件   
        int eventType = parser.getEventType();  
        //只要不是文档结束事件   
        while(eventType!=XmlPullParser.END_DOCUMENT)  
        {  
            switch (eventType)   
            {     
                case XmlPullParser.START_TAG:  
                    //获取解析器当前指向的元素的名称   
                    String name = parser.getName();  
                    if("getMobileCodeInfoResult".equals(name))  
                    {  
                        return parser.nextText();  
                    }  
                    break;  
            }  
            eventType = parser.next();  
        }  
        return null;  
    }  
}  
3.工具类
 
/**
 * 上传文件
 */  
public class FormFile   
{  
    /* 上传文件的数据 */  
    private byte[] data;  
    private InputStream inStream;  
    private File file;  
    /* 文件名称 */  
    private String filname;  
    /* 请求参数名称*/  
    private String parameterName;  
    /* 内容类型 */  
    private String contentType = "application/octet-stream";  
      
    public FormFile(String filname, byte[] data, String parameterName, String contentType)   
    {  
        this.data = data;  
        this.filname = filname;  
        this.parameterName = parameterName;  
        if(contentType!=null) this.contentType = contentType;  
    }  
      
    public FormFile(String filname, File file, String parameterName, String contentType)  
    {  
        this.filname = filname;  
        this.parameterName = parameterName;  
        this.file = file;  
        try   
        {  
            this.inStream = new FileInputStream(file);  
        }   
        catch (FileNotFoundException e)   
        {  
            e.printStackTrace();  
        }  
        if(contentType!=null) this.contentType = contentType;  
    }  
      
    public File getFile()   
    {  
        return file;  
    }  
    public InputStream getInStream()   
    {  
        return inStream;  
    }  
    public byte[] getData()   
    {  
        return data;  
    }  
    public String getFilname()   
    {  
        return filname;  
    }  
    public void setFilname(String filname)  
    {  
        this.filname = filname;  
    }  
    public String getParameterName()   
    {  
        return parameterName;  
    }  
    public void setParameterName(String parameterName)   
    {  
        this.parameterName = parameterName;  
    }  
    public String getContentType()   
    {  
        return contentType;  
    }  
    public void setContentType(String contentType)   
    {  
        this.contentType = contentType;  
    }     
}  

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读