Android:實現手機號歸屬地查找的應用

 

原理:通過HTTP協議發送XML數據並調用webservice(soap)

 

首先定義string.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <resources>  
  4.   
  5.     <string name="app_name">手機號碼歸屬查詢</string>  
  6.   
  7.     <string name="mobile">手機號</string>  
  8.   
  9.     <string name="button">查詢</string>  
  10.   
  11.     <string name="error">連接失敗</string>  
  12.   
  13. </resources>  


 

構建一個佈局:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:layout_width="fill_parent"  
  6.   
  7.     android:layout_height="fill_parent"  
  8.   
  9.     android:orientation="vertical" >  
  10.   
  11.     <TextView  
  12.   
  13.         android:layout_width="fill_parent"  
  14.   
  15.         android:layout_height="wrap_content"  
  16.   
  17.         android:text="@string/mobile" />  
  18.   
  19.     <EditText  
  20.   
  21.         android:id="@+id/mobile"  
  22.   
  23.         android:layout_width="fill_parent"  
  24.   
  25.         android:layout_height="wrap_content"  
  26.   
  27.         />  
  28.   
  29.     <Button  
  30.   
  31.         android:id="@+id/button"  
  32.   
  33.         android:layout_width="fill_parent"  
  34.   
  35.         android:layout_height="wrap_content"  
  36.   
  37.         android:text="@string/button" />  
  38.   
  39.     <TextView  
  40.   
  41.         android:id="@+id/result"  
  42.   
  43.         android:layout_width="fill_parent"  
  44.   
  45.         android:layout_height="wrap_content" />  
  46.   
  47. </LinearLayout>  


 

 


 

效果圖:

 

 

然後編寫activity:

 

 

  1. package cn.csdnmobile;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12. import cn.csdn.service.MobileService;  
  13.   
  14. public class MobileNumberBelongActivity extends Activity implements OnClickListener{  
  15.       
  16.     Button submitBtn ;  
  17.     EditText numberEt;  
  18.     TextView resultTv;  
  19.       
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.           
  24.         findViews();  
  25.     }  
  26.   
  27.     private void findViews() {  
  28.         numberEt = (EditText) this.findViewById(R.id.mobile);  
  29.         submitBtn = (Button) this.findViewById(R.id.button);  
  30.         resultTv = (TextView) this.findViewById(R.id.result);  
  31.           
  32.         submitBtn.setOnClickListener(this);  
  33.     }  
  34.   
  35.     //監聽,判斷並得到數據   
  36.     public void onClick(View v) {  
  37.         String PhoneNumber = numberEt.getText().toString().trim();  
  38.           
  39.         if(!"".equals(PhoneNumber)){  
  40.             try {  
  41.                 //把數據傳給MobileService類中的getMobileBelong()方法   
  42.                 String result = MobileService.getMobileBelong(PhoneNumber);  
  43.                 //顯示經過上面的方法處理過後的數據   
  44.                 resultTv.setText(result);  
  45.             } catch (Exception e) {  
  46.                 Log.e("TAG", e.toString());  
  47.                 Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show();  
  48.             }  
  49.         }  
  50.           
  51.     }  
  52. }  


 

 

 

在得到輸入的數據後,要去交給傳給MobileService類中的getMobileBelong()方法處理,然後再把處理好的數據返回

 

  1. package cn.csdn.service;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5. import java.io.OutputStream;  
  6.   
  7. import java.net.HttpURLConnection;  
  8.   
  9. import java.net.URL;  
  10.   
  11. import org.xmlpull.v1.XmlPullParser;  
  12.   
  13. import android.util.Xml;  
  14.   
  15. import cn.csdn.utils.StreamTool;  
  16.   
  17. public class MobileService {  
  18.   
  19.     public static String getMobileBelong(String mobile) throws Exception{  
  20.   
  21.         //類加載器,調用getResourceAsStream()方法作爲輸入流讀取資源   
  22.   
  23.         InputStream inStream = MobileService.class.getClassLoader().getResourceAsStream("mobile_soap.xml");  
  24.   
  25.         //調用建立的工具類,得到數據   
  26.   
  27.         byte[] data = StreamTool.readInputStream(inStream);  
  28.   
  29.         //重新封裝數據   
  30.   
  31.         String xml = new String(data,"UTF-8");  
  32.   
  33.         /**replaceAll()方法:基於規則表達式的替換 
  34.  
  35.          * 其中用到了正則表達式, 
  36.  
  37.          * "\\$mobile" : 
  38.  
  39.          *  正則表達式$爲正則中的特殊符號須轉義,即\$mobile 
  40.  
  41.          *  而\爲字符串中的特殊符號,所以用兩個反斜槓,即"\\{1}quot; 
  42.  
  43.          * */  
  44.   
  45.         String soapEntity = xml.replaceAll("\\$mobile", mobile);  
  46.   
  47.         data = soapEntity.getBytes();  
  48.   
  49.         //定義發送的目的地址   
  50.   
  51.         String path="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
  52.   
  53.         //建立連接   
  54.   
  55.         URL url = new URL(path);  
  56.   
  57.         // 轉換  HttpURLConnection是專門爲http的連接而設計的類   
  58.   
  59.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  60.   
  61.         //post發送,設置允許得到一個輸入流   
  62.   
  63.         conn.setDoOutput(true);  
  64.   
  65.         //延遲   
  66.   
  67.         conn.setConnectTimeout(5000);  
  68.   
  69.         //請求方法   
  70.   
  71.         conn.setRequestMethod("POST");    
  72.   
  73.         //設置請求頭類型   
  74.   
  75.         conn.setRequestProperty("Content-Type""application/soap+xml;charset=utf-8");    
  76.   
  77.         //設置請求頭長度   
  78.   
  79.         conn.setRequestProperty("Content-Length", String.valueOf(data.length));  
  80.   
  81.           
  82.   
  83.         //發送數據   
  84.   
  85.         OutputStream outStream = conn.getOutputStream();  
  86.   
  87.         outStream.write(data);    
  88.   
  89.         outStream.flush();  
  90.   
  91.         outStream.close();  
  92.   
  93.         //請求成功   
  94.   
  95.         if(conn.getResponseCode() == 200){  
  96.   
  97.             //接收數據   
  98.   
  99.             InputStream responseStream = conn.getInputStream();           
  100.   
  101.             return pareseXml(responseStream);  
  102.   
  103.             }  
  104.   
  105.         return null;  
  106.   
  107.     }  
  108.   
  109.       
  110.   
  111.     //xml解析器   
  112.   
  113.     private static String pareseXml(InputStream responseStream) throws Exception {  
  114.   
  115.           
  116.   
  117.         XmlPullParser parser = Xml.newPullParser();  
  118.   
  119.         parser.setInput(responseStream, "UTF-8");  
  120.   
  121.           
  122.   
  123.         int event = parser.getEventType();  
  124.   
  125.           
  126.   
  127.         while(event != XmlPullParser.END_DOCUMENT){  
  128.   
  129.             switch(event){  
  130.   
  131.             case XmlPullParser.START_TAG:  
  132.   
  133.                 if("getMobileCodeInfoResult".equals(parser.getName())){  
  134.   
  135.                     return parser.nextText();  
  136.   
  137.                 }  
  138.   
  139.                 break;  
  140.   
  141.             }  
  142.   
  143.             event = parser.next();  
  144.   
  145.         }  
  146.   
  147.         return null;  
  148.   
  149.     }  
  150.   
  151. }  


 

 

要調用webservice(soap),所以定義mobile_soap.xml

在MobileService.java 中調用此類轉換的語句:InputStream inStream = MobileService.class.getClassLoader().getResourceAsStream("mobile_soap.xml");

 

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


 

 

在調用mobile_soap.xml 的時候,要把xml格式轉換成字節,所以在這裏要定義一個類

在MobileService.java 中調用此類轉換的語句:byte[] data = StreamTool.readInputStream(inStream);

 

  1. package cn.csdn.utils;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4.   
  5. import java.io.InputStream;  
  6.   
  7. public class StreamTool {  
  8.   
  9.     /** 
  10.  
  11.      * 從輸入流讀取數據 
  12.  
  13.      * 把數據封裝到ByteArrayOutputStream 
  14.  
  15.      * 爲什麼要封裝:可以把它輕而易舉的做一個字節類型的轉換 
  16.  
  17.      *  
  18.  
  19.      * 然後返回 
  20.  
  21.      * @param inStream 
  22.  
  23.      * @return 
  24.  
  25.      * @throws Exception 
  26.  
  27.      */  
  28.   
  29.     public static byte[] readInputStream(InputStream inStream)   
  30.   
  31.             throws Exception {  
  32.   
  33.         ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  34.   
  35.         byte[] buffer = new byte[1024];  
  36.   
  37.         int len = 0;  
  38.   
  39.         while ((len = inStream.read(buffer)) != -1) {  
  40.   
  41.             outSteam.write(buffer, 0, len);  
  42.   
  43.         }  
  44.   
  45.         outSteam.close();  
  46.   
  47.         inStream.close();  
  48.   
  49.         return outSteam.toByteArray();  
  50.   
  51.     }  
  52.   
  53. }  


 

可以實現了。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章