在Android中调用WebService(转载)

出自: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"?>
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
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 }

最后注意,由于需要访问网络,需要加上权限

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
实例代码:http://115.com/file/anjh65a6


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值