本实例将向大家详细介绍,在Android中如何调用服务器端提供的 webservice 获取手机号码归属地信息,实现典型的分布式应用。
01 |
package cn.itcast.mobile.address; |
03 |
import java.io.InputStream; |
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; |
15 |
public class MainActivity extends Activity { |
16 |
private EditText mobileText; |
17 |
private TextView addressView; |
18 |
private static final String TAG = "MainActivity"; |
21 |
public void onCreate(Bundle savedInstanceState) { |
22 |
super.onCreate(savedInstanceState); |
23 |
setContentView(R.layout.main); |
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() { |
30 |
public void onClick(View v) { |
31 |
String mobile = mobileText.getText().toString(); |
32 |
InputStream inStream =this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); |
34 |
addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); |
35 |
} catch (Exception e) { |
36 |
Log.e(TAG, e.toString()); |
37 |
Toast.makeText(MainActivity.this, "查询失败", 1).show(); |
界面文件:
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" |
08 |
Android:layout_width="fill_parent" |
09 |
Android:layout_height="wrap_content" |
10 |
Android:text="@string/mobile" |
14 |
Android:layout_width="fill_parent" |
15 |
Android:layout_height="wrap_content" |
16 |
Android:id="@+id/mobile" |
20 |
Android:layout_width="wrap_content" |
21 |
Android:layout_height="wrap_content" |
22 |
Android:text="@string/button" |
23 |
Android:id="@+id/button" |
27 |
Android:layout_width="fill_parent" |
28 |
Android:layout_height="wrap_content" |
29 |
Android:id="@+id/address" |
重点实现代码:
01 |
package cn.itcast.service; |
03 |
import java.io.InputStream; |
04 |
import java.io.OutputStream; |
05 |
import java.net.HttpURLConnection; |
07 |
import java.util.HashMap; |
09 |
import java.util.regex.Matcher; |
10 |
import java.util.regex.Pattern; |
12 |
import org.xmlpull.v1.XmlPullParser; |
14 |
import Android.util.Xml; |
15 |
import cn.itcast.utils.StreamTool; |
17 |
public class MobileInfoService { |
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); |
29 |
public static String replace(String xml, Map<String, String> params)throwsException{ |
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); |
37 |
result = matcher.replaceAll(entry.getValue()); |
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); |
58 |
if(conn.getResponseCode()==200){ |
59 |
return parseResponseXML(conn.getInputStream()); |
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){//只要不是文档结束事件 |
70 |
case XmlPullParser.START_TAG: |
71 |
String name = parser.getName();//获取解析器当前指向的元素的名称 |
72 |
if("getMobileCodeInfoResult".equals(name)){ |
73 |
return parser.nextText(); |
77 |
eventType = parser.next(); |
最后提醒大家一句,记得在项目清单文件中,加入网络访问权限。
2 |
<uses-permission Android:name="android.permission.INTERNET"/> |