Android發送SOAP數據給服務器調用webservice,實現手機號歸屬地查詢

 

創建android工程MobileBelong,設置網絡訪問權限。

 

資源

  1. <string name="hello">Hello World, MainActivity!</string>  
  2. <string name="app_name">手機號歸屬地查詢</string>  
  3. <string name="mobile">手機號</string>  
  4. <string name="button">查詢</string>  
  5. <string name="error">網絡連接失敗</string>  

佈局

  1. TextView  
  2.         android:layout_width="fill_parent"  
  3.         android:layout_height="wrap_content"  
  4.         android:text="@string/mobile" />  
  5.   
  6.     <EditText  
  7.         android:id="@+id/mobile"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="13472283596" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/button"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/button" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/result"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content" />  

在src目錄下創建mobilesoap.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>  

業務類:MobileService

注意訪問目標地址是:

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx

可以有協議中得到。


  1. package cn.class3g.service;  
  2. …  
  3. public class MobileService {  
  4.   
  5. public static String getMobileAddress(String mobile) throws Exception {  
  6.   
  7.         InputStream inStream = MobileService.class.getClassLoader()  
  8.                 .getResourceAsStream("mobilesoap.xml");  
  9.         byte[] data = StreamTool.readInputStream(inStream);  
  10.         String xml = new String(data);  
  11.         String soap = xml.replaceAll("\\$mobile", mobile);  
  12.   
  13.         /**  
  14.          * 正則表達式$爲特殊正則中的特殊符號須轉義,即\$mobile  
  15.          * 而\爲字符串中的特殊符號,所以用兩個反斜槓,即"\\{1}quot;  
  16.          */  
  17.         String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
  18.         data = soap.getBytes();// 得到了xml的實體數據  
  19.         URL url = new URL(path);  
  20.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  21.         conn.setConnectTimeout(5 * 1000);  
  22.         conn.setRequestMethod("POST");  
  23.         conn.setDoOutput(true);  
  24.         conn.setRequestProperty("Content-Type",  
  25.                 "application/soap+xml; charset=utf-8");  
  26.         conn.setRequestProperty("Content-Length", String.valueOf(data.length));  
  27.         OutputStream outStream = conn.getOutputStream();  
  28.         outStream.write(data);  
  29.         outStream.flush();  
  30.         outStream.close();  
  31.         if (conn.getResponseCode() == 200) {  
  32.             InputStream responseStream = conn.getInputStream();  
  33.             return parseXML(responseStream);  
  34.         }  
  35.         return null;  
  36.     }  
  37.   
  38.     /**  
  39.      * 解析返回xml數據  
  40.      *   
  41.      * @param responseStream  
  42.      * @return  
  43.      * @throws Exception  
  44.      */  
  45.     private static String parseXML(InputStream responseStream) throws Exception {  
  46.         XmlPullParser parser = Xml.newPullParser();  
  47.         parser.setInput(responseStream, "UTF-8");  
  48.         int event = parser.getEventType();  
  49.         while (event != XmlPullParser.END_DOCUMENT) {  
  50.             switch (event) {  
  51.             case XmlPullParser.START_TAG:  
  52.                 if ("getMobileCodeInfoResult".equals(parser.getName())) {  
  53.                     return parser.nextText();  
  54.                 }  
  55.                 break;  
  56.             }  
  57.             event = parser.next();  
  58.         }  
  59.         return null;  
  60.     }  
  61. }  

工具類StreamTool

  1. package cn.class3g.utils;  
  2. …  
  3. public class StreamTool {  
  4.     /**  
  5.      * 從輸入流讀取數據  
  6.      * @param inStream  
  7.      * @return  
  8.      * @throws Exception  
  9.      */  
  10.     public static byte[] readInputStream(InputStream inStream) throws Exception{  
  11.         ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  12.         byte[] buffer = new byte[1024];  
  13.         int len = 0;  
  14.         while( (len = inStream.read(buffer)) !=-1 ){  
  15.             outSteam.write(buffer, 0, len);  
  16.         }  
  17.         outSteam.close();  
  18.         inStream.close();  
  19.         return outSteam.toByteArray();  
  20.     }  
  21. }  
  22.   
  23. Activity類MobileBelongActivity  
  24. package cn.class3g.mobile;  
  25. …  
  26. public class MobileBelongActivity extends Activity {  
  27.   
  28.     private static final String TAG = "MainActivity";  
  29.     private EditText mobileText;  
  30.     private TextView resultView;  
  31.   
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.   
  37.         mobileText = (EditText) this.findViewById(R.id.mobile);  
  38.         resultView = (TextView) this.findViewById(R.id.result);  
  39.         Button button = (Button) this.findViewById(R.id.button);  
  40.         button.setOnClickListener(new View.OnClickListener() {  
  41.             @Override  
  42.             public void onClick(View v) {  
  43.                 String mobile = mobileText.getText().toString();  
  44.                 try {  
  45.                     String address = MobileService.getMobileAddress(mobile);  
  46.                     resultView.setText(address);  
  47.                 } catch (Exception e) {  
  48.                     Log.e(TAG, e.toString());  
  49.                     Toast.makeText(MobileBelongActivity.this, R.string.error, 1).show();  
  50.                 }  
  51.             }  
  52.         });  
  53.     }  
  54. }  


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