Android 开发工具类 31_WebService 获取手机号码归属地

AndroidInteractWithWebService.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <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">
3   <soap12:Body>
4     <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
5       <mobileCode>$mobile</mobileCode>
6       <userID></userID>
7     </getMobileCodeInfo>
8   </soap12:Body>
9 </soap12:Envelope>

WebServiceRequestFromAndroid

 1 package com.wangjialin.internet.service;
 2 
 3 import java.io.InputStream;
 4 import java.net.HttpURLConnection;
 5 import java.net.URL;
 6 
 7 import org.xmlpull.v1.XmlPullParser;
 8 
 9 import android.util.Xml;
10 
11 import com.wangjialin.internet.utils.StreamTool;
12 
13 public class WebServiceRequestFromAndroid {
14     /**
15      * 获取手机号归属地
16      * @param mobile 手机号
17      * @return
18      * @throws Exception
19      */
20     public static String getAddress(String mobile) throws Exception{
21         String soap = readSoap();
22         soap = soap.replaceAll("\\$mobile", mobile);
23         byte[] entity = soap.getBytes();
24         
25         String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
26         HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
27         conn.setConnectTimeout(5000);
28         conn.setRequestMethod("POST");
29         conn.setDoOutput(true);
30         conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
31         conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
32         conn.getOutputStream().write(entity);
33         if(conn.getResponseCode() == 200){
34             return parseSOAP(conn.getInputStream());
35         }
36         return null;
37     }
38     /*
39      <?xml version="1.0" encoding="utf-8"?>
40     <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">
41       <soap12:Body>
42         <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
43           <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
44         </getMobileCodeInfoResponse>
45       </soap12:Body>
46     </soap12:Envelope>
47      */
48     private static String parseSOAP(InputStream xml)throws Exception{
49         XmlPullParser pullParser = Xml.newPullParser();
50         pullParser.setInput(xml, "UTF-8");
51         int event = pullParser.getEventType();
52         while(event != XmlPullParser.END_DOCUMENT){
53             switch (event) {
54             case XmlPullParser.START_TAG:
55                 if("getMobileCodeInfoResult".equals(pullParser.getName())){
56                     return pullParser.nextText();
57                 }
58                 break;
59             }
60             event = pullParser.next();
61         }
62         return null;
63     }
64 
65     private static String readSoap() throws Exception{
66         InputStream inStream = WebServiceRequestFromAndroid.class.getClassLoader().getResourceAsStream("AndroidInteractWithWebService.xml");
67         byte[] data = StreamTool.read(inStream);
68         return new String(data);
69     }
70 }

StreamTool

 1 package com.wangjialin.internet.utils;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.InputStream;
 5 
 6 public class StreamTool {
 7     /**
 8      * 从流中读取数据
 9      * @param inStream
10      * @return
11      */
12     public static byte[] read(InputStream inStream) throws Exception{
13         
14         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
15         byte[] buffer = new byte[1024];
16         int len = 0;
17         while( (len = inStream.read(buffer)) != -1){
18             outputStream.write(buffer, 0, len);
19         }
20         inStream.close();
21         return outputStream.toByteArray();
22     }
23 
24 }

查询

1   <Button  
2     android:layout_width="wrap_content" 
3     android:layout_height="wrap_content" 
4     android:text="@string/button"
5     android:onClick="query"
6     />

 

 1  public void query(View v){
 2             
 3             String mobile = mobileText.getText().toString();
 4             try {
 5                 String address = WebServiceRequestFromAndroid.getAddress(mobile);
 6                 textView.setText(address);
 7             } catch (Exception e) {
 8                 e.printStackTrace();
 9                 Toast.makeText(getApplicationContext(), R.string.error, 1).show();
10             }
11         }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。