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

资源

view plain copy to clipboard print ?
  1. <stringname="hello">HelloWorld,MainActivity!</string>
  2. <stringname="app_name">手机号归属地查询</string>
  3. <stringname="mobile">手机号</string>
  4. <stringname="button">查询</string>
  5. <stringname="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. <EditText
  6. android:id="@+id/mobile"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="13472283596"/>
  10. <Button
  11. android:id="@+id/button"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/button"/>
  15. <TextView
  16. android:id="@+id/result"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"/>

在src目录下创建mobilesoap.xml,并将网址文档中提供的代码复制其中,如下

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <soap12:Envelopexmlns: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. <getMobileCodeInfoxmlns="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. packagecn.class3g.service;
  2. publicclassMobileService{
  3. publicstaticStringgetMobileAddress(Stringmobile)throwsException{
  4. InputStreaminStream=MobileService.class.getClassLoader()
  5. .getResourceAsStream("mobilesoap.xml");
  6. byte[]data=StreamTool.readInputStream(inStream);
  7. Stringxml=newString(data);
  8. Stringsoap=xml.replaceAll("\\$mobile",mobile);
  9. /**
  10. *正则表达式$为特殊正则中的特殊符号须转义,即\$mobile
  11. *而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;
  12. */
  13. Stringpath="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
  14. data=soap.getBytes();//得到了xml的实体数据
  15. URLurl=newURL(path);
  16. HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
  17. conn.setConnectTimeout(5*1000);
  18. conn.setRequestMethod("POST");
  19. conn.setDoOutput(true);
  20. conn.setRequestProperty("Content-Type",
  21. "application/soap+xml;charset=utf-8");
  22. conn.setRequestProperty("Content-Length",String.valueOf(data.length));
  23. OutputStreamoutStream=conn.getOutputStream();
  24. outStream.write(data);
  25. outStream.flush();
  26. outStream.close();
  27. if(conn.getResponseCode()==200){
  28. InputStreamresponseStream=conn.getInputStream();
  29. returnparseXML(responseStream);
  30. }
  31. returnnull;
  32. }
  33. /**
  34. *解析返回xml数据
  35. *
  36. *@paramresponseStream
  37. *@return
  38. *@throwsException
  39. */
  40. privatestaticStringparseXML(InputStreamresponseStream)throwsException{
  41. XmlPullParserparser=Xml.newPullParser();
  42. parser.setInput(responseStream,"UTF-8");
  43. intevent=parser.getEventType();
  44. while(event!=XmlPullParser.END_DOCUMENT){
  45. switch(event){
  46. caseXmlPullParser.START_TAG:
  47. if("getMobileCodeInfoResult".equals(parser.getName())){
  48. returnparser.nextText();
  49. }
  50. break;
  51. }
  52. event=parser.next();
  53. }
  54. returnnull;
  55. }
  56. }
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. packagecn.class3g.utils;
  2. publicclassStreamTool{
  3. /**
  4. *从输入流读取数据
  5. *@paraminStream
  6. *@return
  7. *@throwsException
  8. */
  9. publicstaticbyte[]readInputStream(InputStreaminStream)throwsException{
  10. ByteArrayOutputStreamoutSteam=newByteArrayOutputStream();
  11. byte[]buffer=newbyte[1024];
  12. intlen=0;
  13. while((len=inStream.read(buffer))!=-1){
  14. outSteam.write(buffer,0,len);
  15. }
  16. outSteam.close();
  17. inStream.close();
  18. returnoutSteam.toByteArray();
  19. }
  20. }
  21. Activity类MobileBelongActivity
  22. packagecn.class3g.mobile;
  23. publicclassMobileBelongActivityextendsActivity{
  24. privatestaticfinalStringTAG="MainActivity";
  25. privateEditTextmobileText;
  26. privateTextViewresultView;
  27. @Override
  28. publicvoidonCreate(BundlesavedInstanceState){
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.main);
  31. mobileText=(EditText)this.findViewById(R.id.mobile);
  32. resultView=(TextView)this.findViewById(R.id.result);
  33. Buttonbutton=(Button)this.findViewById(R.id.button);
  34. button.setOnClickListener(newView.OnClickListener(){
  35. @Override
  36. publicvoidonClick(Viewv){
  37. Stringmobile=mobileText.getText().toString();
  38. try{
  39. Stringaddress=MobileService.getMobileAddress(mobile);
  40. resultView.setText(address);
  41. }catch(Exceptione){
  42. Log.e(TAG,e.toString());
  43. Toast.makeText(MobileBelongActivity.this,R.string.error,1).show();
  44. }
  45. }
  46. });
  47. }
  48. }


更多相关文章

  1. Android中构建数据业务应用
  2. 如何查看android数据文件?
  3. android SQLite数据库存储数据
  4. Android获得电话本中的数据(ContentProvide的应用一)
  5. Android 数据库简单操作
  6. Android 数据存储(二) 文件的使用
  7. Android 数据保存

随机推荐

  1. RotateAnimation 设置旋转中心点、不停顿
  2. Android(安卓)Ams浅析
  3. 申请Android(安卓)Maps API Key-及出现的
  4. Android(安卓)数据存储与读取:SQLite
  5. android ICS4.0.3 改变默认字体大小
  6. Android(安卓)实现直接拒接来电
  7. Android:控件布局(相对布局)RelativeLayout
  8. android的apk包签名
  9. Android四种Activity的加载模式
  10. android ConfigChanges