出自:http://www.92coding.com/blog/index.php/archives/375.html
某些情况下我们可能需要与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" ?> |
5 | < mobileCode >$mobile</ mobileCode > |
其次,设计MainActivity布局文件,
main.xml
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
4 | android:orientation = "vertical" |
5 | android:layout_width = "fill_parent" |
6 | android:layout_height = "fill_parent" > |
8 | android:layout_width = "fill_parent" |
9 | android:layout_height = "wrap_content" |
10 | android:text = "手机号码" /> |
12 | android:id = "@+id/mobileNum" |
13 | android:layout_width = "fill_parent" |
14 | android:layout_height = "wrap_content" |
18 | android:id = "@+id/btnSearch" |
19 | android:layout_width = "wrap_content" |
20 | android:layout_height = "wrap_content" |
24 | android:id = "@+id/mobileAddress" |
25 | android:layout_width = "fill_parent" |
26 | android:layout_height = "wrap_content" |
下面贴出MainActivity,
在Android中调用WebService还是比较简单的:请求webservice,获取服务响应的数据,解析后并显示。
1 | package com.szy.webservice; |
3 | import java.io.ByteArrayOutputStream; |
4 | import java.io.InputStream; |
5 | import java.io.OutputStream; |
6 | import java.net.HttpURLConnection; |
8 | import java.util.HashMap; |
10 | import java.util.regex.Matcher; |
11 | import java.util.regex.Pattern; |
13 | import org.xmlpull.v1.XmlPullParser; |
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; |
30 | public class MainActivity extends Activity |
32 | private EditText mobileNum; |
33 | private TextView mobileAddress; |
34 | private static final String TAG = "MainActivity" ; |
37 | public void onCreate(Bundle savedInstanceState) |
39 | super .onCreate(savedInstanceState); |
40 | setContentView(R.layout.main); |
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() |
48 | public void onClick(View v) |
51 | String mobile = mobileNum.getText().toString(); |
53 | InputStream inStream = this .getClass().getClassLoader().getResourceAsStream( "mobilesoap.xml" ); |
57 | mobileAddress.setText(getMobileAddress(inStream, mobile)); |
60 | Log.e(TAG, e.toString()); |
61 | Toast.makeText(MainActivity. this , "查询失败" , 1 ).show(); |
75 | private String getMobileAddress(InputStream inStream, String mobile) throws Exception |
78 | String soap = readSoapFile(inStream, mobile); |
79 | byte [] data = soap.getBytes(); |
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); |
92 | if (conn.getResponseCode() == 200 ) |
95 | return parseResponseXML(conn.getInputStream()); |
100 | private String readSoapFile(InputStream inStream, String mobile) throws Exception |
103 | byte [] data = readInputStream(inStream); |
104 | String soapxml = new String(data); |
106 | Map<String, String> params = new HashMap<String, String>(); |
107 | params.put( "mobile" , mobile); |
109 | return replace(soapxml, params); |
119 | private byte [] readInputStream(InputStream inputStream) throws Exception |
121 | byte [] buffer = new byte [ 1024 ]; |
123 | ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); |
124 | while ((len = inputStream.read(buffer)) != - 1 ) |
126 | outSteam.write(buffer, 0 , len); |
130 | return outSteam.toByteArray(); |
141 | private String replace(String xml, Map<String, String> params) throws Exception |
144 | if (params != null && !params.isEmpty()) |
146 | for (Map.Entry<String, String> entry : params.entrySet()) |
148 | String name = "\\$" + entry.getKey(); |
149 | Pattern pattern = Pattern.compile(name); |
150 | Matcher matcher = pattern.matcher(result); |
153 | result = matcher.replaceAll(entry.getValue()); |
166 | private static String parseResponseXML(InputStream inStream) throws Exception |
168 | XmlPullParser parser = Xml.newPullParser(); |
169 | parser.setInput(inStream, "UTF-8" ); |
170 | int eventType = parser.getEventType(); |
171 | while (eventType != XmlPullParser.END_DOCUMENT) |
176 | case XmlPullParser.START_TAG: |
177 | String name = parser.getName(); |
178 | if ( "getMobileCodeInfoResult" .equals(name)) |
180 | return parser.nextText(); |
184 | eventType = parser.next(); |
最后注意,由于需要访问网络,需要加上权限
1 | < uses-permission android:name = "android.permission.INTERNET" /> |
通过上面简单的例子,相信大家已经学习了如何在Android中调用WebService,最后运行效果:
<a href="https://i-blog.csdnimg.cn/blog_migrate/d8c1a24ab86484ca536321829bc044aa.png" class="cboxElement" rel="example4" 375"="" style="text-decoration: initial; color: rgb(1, 150, 227);">![Android_WebService Android调用WebService](https://i-blog.csdnimg.cn/blog_migrate/d8c1a24ab86484ca536321829bc044aa.png)
实例代码:http://115.com/file/anjh65a6