歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android手機歸屬地查詢工具

Android手機歸屬地查詢工具

日期:2017/3/1 11:12:54   编辑:Linux編程

在Android應用中,我們經常會與網絡上的服務端的程序(J2EE或者.NET等應用)進行交互,通信。

本實例將向大家詳細介紹,在android中如何調用服務器端提供的webservice,實現典型的分布式應用。

  1. package cn.itcast.mobile.address;
  2. import java.io.InputStream;
  3. import cn.itcast.service.MobileInfoService;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12. public class MainActivity extends Activity {
  13. private EditText mobileText;
  14. private TextView addressView;
  15. private static final String TAG = "MainActivity";
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. mobileText = (EditText)this.findViewById(R.id.mobile);
  21. addressView = (TextView)this.findViewById(R.id.address);
  22. Button button = (Button)this.findViewById(R.id.button);
  23. button.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. String mobile = mobileText.getText().toString();
  27. InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");
  28. try {
  29. addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile));
  30. } catch (Exception e) {
  31. Log.e(TAG, e.toString());
  32. Toast.makeText(MainActivity.this, "查詢失敗", 1).show();
  33. }
  34. }
  35. });
  36. }
  37. }
界面文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/mobile"
  11. />
  12. <EditText
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:id="@+id/mobile"
  16. />
  17. <Button
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="@string/button"
  21. android:id="@+id/button"
  22. />
  23. <TextView
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:id="@+id/address"
  27. />
  28. </LinearLayout>
重點實現代碼:
  1. package cn.itcast.service;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import org.xmlpull.v1.XmlPullParser;
  11. import android.util.Xml;
  12. import cn.itcast.utils.StreamTool;
  13. public class MobileInfoService {
  14. private static String readSoapFile(InputStream inStream, String mobile) throws Exception{
  15. byte[] data = StreamTool.readInputStream(inStream);
  16. String soapxml = new String(data);
  17. Map<String, String> params = new HashMap<String, String>();
  18. params.put("mobile", mobile);
  19. return replace(soapxml, params);
  20. }
  21. public static String replace(String xml, Map<String, String> params)throws Exception{
  22. String result = xml;
  23. if(params!=null && !params.isEmpty()){
  24. for(Map.Entry<String, String> entry : params.entrySet()){
  25. String name = "\\{1}quot;+ entry.getKey();
  26. Pattern pattern = Pattern.compile(name);
  27. Matcher matcher = pattern.matcher(result);
  28. if(matcher.find()){
  29. result = matcher.replaceAll(entry.getValue());
  30. }
  31. }
  32. }
  33. return result;
  34. }
  35. public static String getMobileAddress(InputStream inStream, String mobile)throws Exception{
  36. String soap = readSoapFile(inStream, mobile);
  37. byte[] data = soap.getBytes();
  38. URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
  39. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  40. conn.setRequestMethod("POST");
  41. conn.setConnectTimeout(5 * 1000);
  42. conn.setDoOutput(true);//如果通過post提交數據,必須設置允許對外輸出數據
  43. conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
  44. conn.setRequestProperty("Content-Length", String.valueOf(data.length));
  45. OutputStream outStream = conn.getOutputStream();
  46. outStream.write(data);
  47. outStream.flush();
  48. outStream.close();
  49. if(conn.getResponseCode()==200){
  50. return parseResponseXML(conn.getInputStream());
  51. }
  52. return null;
  53. }
  54. private static String parseResponseXML(InputStream inStream) throws Exception{
  55. XmlPullParser parser = Xml.newPullParser();
  56. parser.setInput(inStream, "UTF-8");
  57. int eventType = parser.getEventType();//產生第一個事件
  58. while(eventType!=XmlPullParser.END_DOCUMENT){//只要不是文檔結束事件
  59. switch (eventType) {
  60. case XmlPullParser.START_TAG:
  61. String name = parser.getName();//獲取解析器當前指向的元素的名稱
  62. if("getMobileCodeInfoResult".equals(name)){
  63. return parser.nextText();
  64. }
  65. break;
  66. }
  67. eventType = parser.next();
  68. }
  69. return null;
  70. }
  71. }
最後提醒大家一句,記得在項目清單文件中,加入網絡訪問權限。

<!-- 訪問網絡的權限 -->
<uses-permission android:name="android.permission.INTERNET"/>

Copyright © Linux教程網 All Rights Reserved