某些情況下我們可能需要與Mysql或者Oracle數據庫進行數據交互,有些朋友的第一反應就是直接在Android中加載驅動然后進行數據的增刪改查。我個人不推薦這種做法,一是手機畢竟不是電腦,操作大量數據費時費電;二是流量貴如金那。我個人比較推薦的做法是使用Java或PHP等開發接口或者編寫WebService進行數據庫的增刪該查,然后Android調用接口或者WebService進行數據的交互。本文就給大家講解在Android中如何調用遠程服務器端提供的WebService。
既然是調用WebService,我們首先的搭建WebService服務器。為了便於操作,我們就使用網上免費的WebService進行學習。
地址:http://www.webxml.com.cn/zh_cn/index.aspx
下面演示的就是如何通過該網站提供的手機號碼歸屬地查詢WebService服務查詢號碼歸屬地
調用地址http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo。
首先,將請求消息保存在XML文件中,然后使用$替換請求參數,如下:
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>
其次,設計MainActivity布局文件,
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="fill_parent" 6 android:layout_height="fill_parent"> 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="手機號碼" /> 11 <EditText 12 android:id="@+id/mobileNum" 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:text="" 16 /> 17 <Button 18 android:id="@+id/btnSearch" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="查詢" 22 /> 23 <TextView 24 android:id="@+id/mobileAddress" 25 android:layout_width="fill_parent" 26 android:layout_height="wrap_content" 27 /> 28 </LinearLayout>
下面貼出MainActivity,
在Android中調用WebService還是比較簡單的:請求webservice,獲取服務響應的數據,解析后並顯示。
1 package com.szy.webservice; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import java.util.HashMap; 9 import java.util.Map; 10 import java.util.regex.Matcher; 11 import java.util.regex.Pattern; 12 13 import org.xmlpull.v1.XmlPullParser; 14 15 import android.app.Activity; 16 import android.os.Bundle; 17 import android.util.Log; 18 import android.util.Xml; 19 import android.view.View; 20 import android.widget.Button; 21 import android.widget.EditText; 22 import android.widget.TextView; 23 import android.widget.Toast; 24 25 /** 26 * @author coolszy 27 * @date 2012-3-8 28 * @blog http://blog.92coding.com 29 */ 30 public class MainActivity extends Activity 31 { 32 private EditText mobileNum; 33 private TextView mobileAddress; 34 private static final String TAG = "MainActivity"; 35 36 @Override 37 public void onCreate(Bundle savedInstanceState) 38 { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.main); 41 42 mobileNum = (EditText) this.findViewById(R.id.mobileNum); 43 mobileAddress = (TextView) this.findViewById(R.id.mobileAddress); 44 Button btnSearch = (Button) this.findViewById(R.id.btnSearch); 45 btnSearch.setOnClickListener(new View.OnClickListener() 46 { 47 @Override 48 public void onClick(View v) 49 { 50 // 獲取電話號碼 51 String mobile = mobileNum.getText().toString(); 52 // 讀取xml文件 53 InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); 54 try 55 { 56 // 顯示電話號碼地理位置,該段代碼不合理,僅供參考 57 mobileAddress.setText(getMobileAddress(inStream, mobile)); 58 } catch (Exception e) 59 { 60 Log.e(TAG, e.toString()); 61 Toast.makeText(MainActivity.this, "查詢失敗", 1).show(); 62 } 63 } 64 }); 65 } 66 67 /** 68 * 獲取電話號碼地理位置 69 * 70 * @param inStream 71 * @param mobile 72 * @return 73 * @throws Exception 74 */ 75 private String getMobileAddress(InputStream inStream, String mobile) throws Exception 76 { 77 // 替換xml文件中的電話號碼 78 String soap = readSoapFile(inStream, mobile); 79 byte[] data = soap.getBytes(); 80 // 提交Post請求 81 URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); 82 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 83 conn.setRequestMethod("POST"); 84 conn.setConnectTimeout(5 * 1000); 85 conn.setDoOutput(true); 86 conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); 87 conn.setRequestProperty("Content-Length", String.valueOf(data.length)); 88 OutputStream outStream = conn.getOutputStream(); 89 outStream.write(data); 90 outStream.flush(); 91 outStream.close(); 92 if (conn.getResponseCode() == 200) 93 { 94 // 解析返回信息 95 return parseResponseXML(conn.getInputStream()); 96 } 97 return "Error"; 98 } 99 100 private String readSoapFile(InputStream inStream, String mobile) throws Exception 101 { 102 // 從流中獲取文件信息 103 byte[] data = readInputStream(inStream); 104 String soapxml = new String(data); 105 // 占位符參數 106 Map<String, String> params = new HashMap<String, String>(); 107 params.put("mobile", mobile); 108 // 替換文件中占位符 109 return replace(soapxml, params); 110 } 111 112 /** 113 * 讀取流信息 114 * 115 * @param inputStream 116 * @return 117 * @throws Exception 118 */ 119 private byte[] readInputStream(InputStream inputStream) throws Exception 120 { 121 byte[] buffer = new byte[1024]; 122 int len = -1; 123 ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); 124 while ((len = inputStream.read(buffer)) != -1) 125 { 126 outSteam.write(buffer, 0, len); 127 } 128 outSteam.close(); 129 inputStream.close(); 130 return outSteam.toByteArray(); 131 } 132 133 /** 134 * 替換文件中占位符 135 * 136 * @param xml 137 * @param params 138 * @return 139 * @throws Exception 140 */ 141 private String replace(String xml, Map<String, String> params) throws Exception 142 { 143 String result = xml; 144 if (params != null && !params.isEmpty()) 145 { 146 for (Map.Entry<String, String> entry : params.entrySet()) 147 { 148 String name = "\\$" + entry.getKey(); 149 Pattern pattern = Pattern.compile(name); 150 Matcher matcher = pattern.matcher(result); 151 if (matcher.find()) 152 { 153 result = matcher.replaceAll(entry.getValue()); 154 } 155 } 156 } 157 return result; 158 } 159 160 /** 161 * 解析XML文件 162 * @param inStream 163 * @return 164 * @throws Exception 165 */ 166 private static String parseResponseXML(InputStream inStream) throws Exception 167 { 168 XmlPullParser parser = Xml.newPullParser(); 169 parser.setInput(inStream, "UTF-8"); 170 int eventType = parser.getEventType();// 產生第一個事件 171 while (eventType != XmlPullParser.END_DOCUMENT) 172 { 173 // 只要不是文檔結束事件 174 switch (eventType) 175 { 176 case XmlPullParser.START_TAG: 177 String name = parser.getName();// 獲取解析器當前指向的元素的名稱 178 if ("getMobileCodeInfoResult".equals(name)) 179 { 180 return parser.nextText(); 181 } 182 break; 183 } 184 eventType = parser.next(); 185 } 186 return null; 187 } 188 }
最后注意,由於需要訪問網絡,需要加上權限
<uses-permission android:name="android.permission.INTERNET"/>

