Android发送SOAP数据给服务器调用webservice,实现手机号归属地查询

 

创建android工程MobileBelong,设置网络访问权限。

 

资源

view plain copy to clipboard print ?
  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>  

布局

view plain copy to clipboard print ?
  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,并将网址文档中提供的代码复制其中,如下

view plain copy to clipboard print ?
  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

可以有协议中得到。


view plain copy to clipboard print ?
  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. }  
package cn.class3g.service; … public class MobileService { public static String getMobileAddress(String mobile) throws Exception { InputStream inStream = MobileService.class.getClassLoader() .getResourceAsStream("mobilesoap.xml"); byte[] data = StreamTool.readInputStream(inStream); String xml = new String(data); String soap = xml.replaceAll("\\$mobile", mobile); /** * 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile * 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot; */ String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; data = soap.getBytes();// 得到了xml的实体数据 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("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) { InputStream responseStream = conn.getInputStream(); return parseXML(responseStream); } return null; } /** * 解析返回xml数据 * * @param responseStream * @return * @throws Exception */ private static String parseXML(InputStream responseStream) throws Exception { XmlPullParser parser = Xml.newPullParser(); parser.setInput(responseStream, "UTF-8"); int event = parser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_TAG: if ("getMobileCodeInfoResult".equals(parser.getName())) { return parser.nextText(); } break; } event = parser.next(); } return null; } }

工具类StreamTool

view plain copy to clipboard print ?
  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. }  


你可能感兴趣的:(android,webservice,服务器,mobile,SOAP,手机)