本文共 8422 字,大约阅读时间需要 28 分钟。
1:原理解析
我们通过soap协议来调用
android端需要发送的数据信息,也是协议信息
- POST /WebServices/MobileCodeWS.asmx HTTP/1.1
- Host: webservice.webxml.com.cn
- Content-Type: application/soap+xml; charset=utf-8
- Content-Length: length
-
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <soap12:Body>
- <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
- <mobileCode><span style="color:#ff0000;">string</span></mobileCode>
- <userID><span style="color:#ff0000;">string</span></userID>
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
以上是通过发送POST协议来调用webservice,通过下面定义的xml数据来调用服务器端的webservice,在下面的xml数据中,被红色标识的部分需要用户来填充,mobileCode是手机号码,userID为用户的id
服务器端返回的信息:
- HTTP/1.1 200 OK
- Content-Type: application/soap+xml; charset=utf-8
- Content-Length: length
-
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <soap12:Body>
- <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
- <span style="color:#ff0000;"><getMobileCodeInfoResult>string</getMobileCodeInfoResult></span>
- </getMobileCodeInfoResponse>
- </soap12:Body>
- </soap12:Envelope>
以上是服务器端返回的信息,标识部分代表返回的号码归属地的信息,可以通过解析xml数据来获得相关的信息
android代码
2:用到的资源文件 strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, MainActivity!</string>
- <string name="app_name">手机号归属地查询</string>
- <string name="mobile">手机号</string>
- <string name="button">查询</string>
- <string name="show">在此显示查询结果</string>
- </resources>
3:布局文件main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/mobile"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/mobile"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:text="@string/button"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/address"
- android:text="@string/show"
- />
- </LinearLayout>
4:MainActivity的内容
- package com.capinfotech.mobileaddress;
-
- import java.io.InputStream;
-
- import com.capinfotech.mobile.service.MobileService;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
- private static final String TAG = "MainActivity";
- private EditText mobileEditText = null;
- private Button addressButton = null;
- private TextView addressResult = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mobileEditText = (EditText)findViewById(R.id.mobile);
- addressButton = (Button)findViewById(R.id.button);
- addressResult = (TextView)findViewById(R.id.address);
-
- addressButton.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- String mobile = mobileEditText.getText().toString();
- InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("mobileaddress.xml");
- Log.i(TAG, "inputStream: " + (inputStream == null));
-
- try {
- Log.i(TAG, "inputStream: " + inputStream.available());
- String address = MobileService.getMobileAddress(inputStream, mobile);
- Log.i(TAG, "address: " + address);
- addressResult.setText(address);
- } catch(Exception e) {
- Log.e(TAG, e.toString());
- Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- }
5:MobileService类的内容
- package com.capinfotech.mobile.service;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- import org.xmlpull.v1.XmlPullParser;
- import org.xmlpull.v1.XmlPullParserException;
-
- import com.capinfotech.mobile.utils.StreamTool;
-
- import android.util.Log;
- import android.util.Xml;
-
- public class MobileService {
- private static final String TAG = "MobileService";
-
- public static String readSoapFile(InputStream inputStream, String mobile) throws Exception {
- byte[] data = StreamTool.readInputStream(inputStream);
- Log.i(TAG, "datalength: " + data.length);
- String soapxml = new String(data);
- Log.i(TAG, "soapxml: " + soapxml);
- Map<String, String> params = new HashMap<String, String>();
- params.put("mobile", mobile);
- return replace(soapxml, params);
-
- }
-
-
- private static String replace(String xml, Map<String, String> params) throws Exception {
- String result = xml;
- if(params != null && !params.isEmpty()) {
- for(Map.Entry<String, String> entry: params.entrySet()) {
- String name = "\\{ 1}quot; + entry.getKey();
- Pattern pattern = Pattern.compile(name);
- Matcher matcher = pattern.matcher(result);
- if(matcher.find()) {
- result = matcher.replaceAll(entry.getValue());
- }
- }
- }
- return result;
- }
-
- public static String getMobileAddress(InputStream inStream, String mobile) throws Exception {
- String soap = readSoapFile(inStream, mobile);
- byte[] data = soap.getBytes();
- URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setDoOutput(true);
- conn.setConnectTimeout(5 * 1000);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
- conn.setRequestProperty("Content-Length", String.valueOf(data.length));
-
- OutputStream outputStream = conn.getOutputStream();
- outputStream.write(data);
- outputStream.flush();
- outputStream.close();
-
- if(conn.getResponseCode() == 200) {
- return parseResponseXML(conn.getInputStream());
- }
-
- return null;
- }
-
-
- private static String parseResponseXML(InputStream inputStream) throws XmlPullParserException, IOException {
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(inputStream, "UTF-8");
- int eventType = parser.getEventType();
- while(eventType != XmlPullParser.END_DOCUMENT) {
- switch(eventType) {
- case XmlPullParser.START_TAG:
- String name = parser.getName();
- if("getMobileCodeInfoResult".equals(name)) {
- return parser.nextText();
- }
- break;
- }
- eventType = parser.next();
- }
- return null;
- }
-
- }
6:StreamTool类的内容
- package com.capinfotech.mobile.utils;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
-
- public class StreamTool {
-
-
-
-
- public static byte[] readInputStream(InputStream inputStream) throws IOException {
- byte[] buffer = new byte[1024];
- int len = 0;
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- while((len = inputStream.read(buffer)) != -1) {
- bos.write(buffer, 0, len);
- }
- bos.close();
- return bos.toByteArray();
-
- }
- }
7:mobileaddress.xml的内容
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <soap12:Body>
- <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
- <mobileCode>$mobile</mobileCode>
- <userID></userID>
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
8:androidmanifest.xml的内容
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.capinfotech.mobileaddress"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- </application>
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.INTERNET" />
-
- </manifest>
9:程序运行效果图:
转载地址:http://fclvi.baihongyu.com/