Android 開發工具類 31_WebService 擷取手機號碼歸屬地

來源:互聯網
上載者:User

標籤:

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      * @return18      * @throws Exception19      */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 inStream10      * @return11      */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         }

 

Android 開發工具類 31_WebService 擷取手機號碼歸屬地

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.