此app的实现功能如图所示:
注:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx是本文webservice的提供商
具体的用法见:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
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>string</mobileCode>
<userID>string</userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>
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/">
<getMobileCodeInfoResult>string</getMobileCodeInfoResult>
</getMobileCodeInfoResponse>
</soap12:Body>
</soap12:Envelope>
step1:创建android工程MobileAddressQuery,并设置网络访问权限。
step2:设计应用的UI界面和使用的字符串值
/res/values/string.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="error">查询失败</string>
</resources>
布局 /res/layout/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="fill_parent"
android:layout_height="wrap_content" android:text="@string/button"
android:onClick="query" /><!-- 指定button的响应方法为query -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/textView" />
</LinearLayout>
step3:在src目录下创建sop12.xml,并将网址文档中提供的代码复制其中,如下
step4:业务类:cn.roco.address.AddressService
注意访问目标地址是:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
可以有协议中得到。
package cn.roco.address.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
import cn.roco.utils.StreamTools;
public class AddressService {
/**
* 用于获取手机号归属地 调用webservice
* @param mobile 手机号
* @return
* @throws Exception
*/
public static String getAddress(String mobile) throws Exception {
String soap = readSoap();
/**
* 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile
* 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;
*/
soap = soap.replaceAll("\\$mobile", mobile); //将用户输入的手机号码代替sop12.xml中的$mobile
byte[] entity = soap.getBytes();
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; //webservice提供源
HttpURLConnection connection = (HttpURLConnection) new URL(path)
.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/soap+xml; charset=utf-8");
connection.setRequestProperty("Content-Length",
String.valueOf(entity.length));
connection.setDoOutput(true);
connection.getOutputStream().write(entity);
if (connection.getResponseCode() == 200) {
return parseSOAP(connection.getInputStream());
}
return null;
}
/**
* 解析webservice返回的数据
* <?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/">
<getMobileCodeInfoResult>string</getMobileCodeInfoResult>
</getMobileCodeInfoResponse>
</soap12:Body>
</soap12:Envelope>
* @param inputStream
* @return
* @throws Exception
*/
private static String parseSOAP(InputStream xml) throws Exception {
XmlPullParser pullParser=Xml.newPullParser();
pullParser.setInput(xml,"UTF-8");
int event=pullParser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_TAG:
if ("getMobileCodeInfoResult".equals(pullParser.getName())) {
return pullParser.nextText();
}
break;
}
event=pullParser.next();
}
return null;
}
/**
* 读取sop12.xml的内容
* @return
* @throws Exception
*/
private static String readSoap() throws Exception {
InputStream inputStream = AddressService.class.getClassLoader()
.getResourceAsStream("sop12.xml");
byte[] data = StreamTools.read(inputStream);
return new String(data);
}
}
step5:工具类cn.roco.utils.StreamTool
package cn.roco.utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTools {
/**
* 读取输入流中的数据
* @param inputStream 输入流
* @return 二进制的流数据
* @throws Exception
*/
public static byte[] read(InputStream inputStream) throws Exception {
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int length=0;
while((length=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,length);
}
inputStream.close();
return outputStream.toByteArray();
}
}
step6:cn.roco.address.MainActivity
package cn.roco.address;
import cn.roco.address.service.AddressService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText mobileText;
private TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mobileText = (EditText) this.findViewById(R.id.mobile);
textView = (TextView) this.findViewById(R.id.textView);
}
public void query(View v) {
String mobile=mobileText.getText().toString();
try {
String address=AddressService.getAddress(mobile);
textView.setText(address);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
}
}
step7:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.roco.address" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<!-- 访问Internet权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<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>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:targetPackage="cn.roco.address"
android:name="android.test.InstrumentationTestRunner" />
</manifest>
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
分享到:
相关推荐
Android中调用Webservice实现号码归属地查询案例!
在android端使用了soap协议来调用网上的webservice接口,来获得手机号码的归属地信息
CSharp调用WebService实现手机号码归属地查询
C# 调用WebService查询手机号码归属地
android基于webservice的手机号码查询功能,自己学习的时候写的小例子,简单易懂!android基于webservice的手机号码查询功能,自己学习的时候写的小例子,简单易懂!
Android调用webservice查询手机归属地信息,可以直接运行,非常的简单好用
NULL 博文链接:https://phantomes.iteye.com/blog/1698593
Android号码归属地查询源码,调用webservice方法来实现。
我第一次使用webservice传输数据的时候用来参考的例子,里面代码很清晰,适合做参考,而且实现了号码归属地的查询,代码简洁。
Android中ksoap2-android调用WebService 实现天气预报,4.0版本好像不能运行,但是可以参考学习。
Android 利用axis2 调用webservice 包含j2ee服务端代码和Android客户端代码 代码实现包含 List,map ,对象传递
通过此可以对android调用webservice有一个更深刻的认识
android调用.net的webservice的例子获取手机的归属地
Andriod调用WebService查询手机归属地
winform调用webservice 手机号码所在地查询,应用了webxml网站所提供的webservice接口,源代码+安装程序。 请勿用于商业用途。
android中调用webservice:ksoap2.jar
Android高级应用源码-Android调用Webservice.zip
Android调用WebService,请参看博客《Android调用WebService之客户端实现(二)》
java android 调用webservice,java android 调用webservice
调用服务器的webservice接口,实现从Android上传图片到服务器,然后从服务器下载图片到Android客户端 从Android端用io流读取到要上传的图片,用Base64编码成字节流的字符串,通过调用webservice把该字符串作为参数...