本实例将向大家详细介绍,在Android中如何调用服务器端提供的 webservice 获取手机号码归属地信息,实现典型的分布式应用。
01
package cn.itcast.mobile.address;
02
03
import java.io.InputStream;
04
05
import cn.itcast.service.MobileInfoService;
06
import Android.app.Activity;
07
import Android.os.Bundle;
08
import Android.util.Log;
09
import Android.view.View;
10
import Android.widget.Button;
11
import Android.widget.EditText;
12
import Android.widget.TextView;
13
import Android.widget.Toast;
14
15
public class MainActivity extends Activity {
16
private EditText mobileText;
17
private TextView addressView;
18
private static final String TAG = "MainActivity";
19
20
@Override
21
public void onCreate(Bundle savedInstanceState) {
22
super.onCreate(savedInstanceState);
23
setContentView(R.layout.main);
24
25
mobileText = (EditText)this.findViewById(R.id.mobile);
26
addressView = (TextView)this.findViewById(R.id.address);
27
Button button = (Button)this.findViewById(R.id.button);
28
button.setOnClickListener(new View.OnClickListener() {
29
@Override
30
public void onClick(View v) {
31
String mobile = mobileText.getText().toString();
32
InputStream inStream =this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");
33
try {
34
addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile));
35
} catch (Exception e) {
36
Log.e(TAG, e.toString());
37
Toast.makeText(MainActivity.this, "查询失败", 1).show();
38
}
39
}
40
});
41
}
42
}
界面文件:
01
<?xml version="1.0" encoding="utf-8"?>
02
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
03
Android:orientation="vertical"
04
Android:layout_width="fill_parent"
05
Android:layout_height="fill_parent"
06
>
07
<TextView
08
Android:layout_width="fill_parent"
09
Android:layout_height="wrap_content"
10
Android:text="@string/mobile"
11
/>
12
13
<EditText
14
Android:layout_width="fill_parent"
15
Android:layout_height="wrap_content"
16
Android:id="@+id/mobile"
17
/>
18
19
<Button
20
Android:layout_width="wrap_content"
21
Android:layout_height="wrap_content"
22
Android:text="@string/button"
23
Android:id="@+id/button"
24
/>
25
26
<TextView
27
Android:layout_width="fill_parent"
28
Android:layout_height="wrap_content"
29
Android:id="@+id/address"
30
/>
31
</LinearLayout>
重点实现代码:
01
package cn.itcast.service;
02
03
import java.io.InputStream;
04
import java.io.OutputStream;
05
import java.net.HttpURLConnection;
06
import java.net.URL;
07
import java.util.HashMap;
08
import java.util.Map;
09
import java.util.regex.Matcher;
10
import java.util.regex.Pattern;
11
12
import org.xmlpull.v1.XmlPullParser;
13
14
import Android.util.Xml;
15
import cn.itcast.utils.StreamTool;
16
17
public class MobileInfoService {
18
19
20
private static String readSoapFile(InputStream inStream, String mobile)throws Exception{
21
byte[] data = StreamTool.readInputStream(inStream);
22
String soapxml = new String(data);
23
Map<String, String> params = new HashMap<String, String>();
24
params.put("mobile", mobile);
25
return replace(soapxml, params);
26
}
27
28
29
public static String replace(String xml, Map<String, String> params)throwsException{
30
String result = xml;
31
if(params!=null && !params.isEmpty()){
32
for(Map.Entry<String, String> entry : params.entrySet()){
33
String name = "\\{1}quot;+ entry.getKey();
34
Pattern pattern = Pattern.compile(name);
35
Matcher matcher = pattern.matcher(result);
36
if(matcher.find()){
37
result = matcher.replaceAll(entry.getValue());
38
}
39
}
40
}
41
return result;
42
}
43
44
public static String getMobileAddress(InputStream inStream, String mobile)throws Exception{
45
String soap = readSoapFile(inStream, mobile);
46
byte[] data = soap.getBytes();
47
URL url = newURL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
48
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
49
conn.setRequestMethod("POST");
50
conn.setConnectTimeout(5 * 1000);
51
conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据
52
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
53
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
54
OutputStream outStream = conn.getOutputStream();
55
outStream.write(data);
56
outStream.flush();
57
outStream.close();
58
if(conn.getResponseCode()==200){
59
return parseResponseXML(conn.getInputStream());
60
}
61
return null;
62
}
63
64
private static String parseResponseXML(InputStream inStream) throwsException{
65
XmlPullParser parser = Xml.newPullParser();
66
parser.setInput(inStream, "UTF-8");
67
int eventType = parser.getEventType();//产生第一个事件
68
while(eventType!=XmlPullParser.END_DOCUMENT){//只要不是文档结束事件
69
switch (eventType) {
70
case XmlPullParser.START_TAG:
71
String name = parser.getName();//获取解析器当前指向的元素的名称
72
if("getMobileCodeInfoResult".equals(name)){
73
return parser.nextText();
74
}
75
break;
76
}
77
eventType = parser.next();
78
}
79
return null;
80
}
81
}
最后提醒大家一句,记得在项目清单文件中,加入网络访问权限。
1
<!-- 访问网络的权限 -->
2
<uses-permission Android:name="android.permission.INTERNET"/>