Android发送SOAP数据给服务器调用webservice,实现手机号归属地查询
2014-03-02 19:37
459 查看
/content/8377428.html
创建android工程MobileBelong,设置网络访问权限。
资源
view
plaincopy
to clipboard
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">手机号归属地查询</string>
<string name="mobile">手机号</string>
<string name="button">查询</string>
<string name="error">网络连接失败</string>
布局
view
plaincopy
to clipboard
TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile" />
<EditText
android:id="@+id/mobile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="13472283596" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
在src目录下创建mobilesoap.xml,并将网址文档中提供的代码复制其中,如下
view
plaincopy
to clipboard
<?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>
业务类:MobileService
注意访问目标地址是:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
可以有协议中得到。
view
plaincopy
to clipboard
package cn.class3g.service;
…
public class MobileService {
public static String getMobileAddress(String mobile) throws Exception {
InputStream inStream = MobileService.class.getClassLoader()
.getResourceAsStream("mobilesoap.xml");
byte[] data = StreamTool.readInputStream(inStream);
String xml = new String(data);
String soap = xml.replaceAll("\\$mobile", mobile);
/**
* 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile
* 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;
*/
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
data = soap.getBytes();// 得到了xml的实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
"application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == 200) {
InputStream responseStream = conn.getInputStream();
return parseXML(responseStream);
}
return null;
}
/**
* 解析返回xml数据
*
* @param responseStream
* @return
* @throws Exception
*/
private static String parseXML(InputStream responseStream) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(responseStream, "UTF-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_TAG:
if ("getMobileCodeInfoResult".equals(parser.getName())) {
return parser.nextText();
}
break;
}
event = parser.next();
}
return null;
}
}
[html] view
plaincopy
package cn.class3g.service;
…
public class MobileService {
public static String getMobileAddress(String mobile) throws Exception {
InputStream inStream = MobileService.class.getClassLoader()
.getResourceAsStream("mobilesoap.xml");
byte[] data = StreamTool.readInputStream(inStream);
String xml = new String(data);
String soap = xml.replaceAll("\\$mobile", mobile);
/**
* 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile
* 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;
*/
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
data = soap.getBytes();// 得到了xml的实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
"application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == 200) {
InputStream responseStream = conn.getInputStream();
return parseXML(responseStream);
}
return null;
}
/**
* 解析返回xml数据
*
* @param responseStream
* @return
* @throws Exception
*/
private static String parseXML(InputStream responseStream) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(responseStream, "UTF-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_TAG:
if ("getMobileCodeInfoResult".equals(parser.getName())) {
return parser.nextText();
}
break;
}
event = parser.next();
}
return null;
}
}
工具类StreamTool
view
plaincopy
to clipboard
package cn.class3g.utils;
…
public class StreamTool {
/**
* 从输入流读取数据
* @param inStream
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) !=-1 ){
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
}
Activity类MobileBelongActivity
package cn.class3g.mobile;
…
public class MobileBelongActivity extends Activity {
private static final String TAG = "MainActivity";
private EditText mobileText;
private TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mobileText = (EditText) this.findViewById(R.id.mobile);
resultView = (TextView) this.findViewById(R.id.result);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mobile = mobileText.getText().toString();
try {
String address = MobileService.getMobileAddress(mobile);
resultView.setText(address);
} catch (Exception e) {
Log.e(TAG, e.toString());
Toast.makeText(MobileBelongActivity.this, R.string.error, 1).show();
}
}
});
}
}
创建android工程MobileBelong,设置网络访问权限。
资源
view
plaincopy
to clipboard
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">手机号归属地查询</string>
<string name="mobile">手机号</string>
<string name="button">查询</string>
<string name="error">网络连接失败</string>
布局
view
plaincopy
to clipboard
TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile" />
<EditText
android:id="@+id/mobile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="13472283596" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
在src目录下创建mobilesoap.xml,并将网址文档中提供的代码复制其中,如下
view
plaincopy
to clipboard
<?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>
业务类:MobileService
注意访问目标地址是:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
可以有协议中得到。
view
plaincopy
to clipboard
package cn.class3g.service;
…
public class MobileService {
public static String getMobileAddress(String mobile) throws Exception {
InputStream inStream = MobileService.class.getClassLoader()
.getResourceAsStream("mobilesoap.xml");
byte[] data = StreamTool.readInputStream(inStream);
String xml = new String(data);
String soap = xml.replaceAll("\\$mobile", mobile);
/**
* 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile
* 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;
*/
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
data = soap.getBytes();// 得到了xml的实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
"application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == 200) {
InputStream responseStream = conn.getInputStream();
return parseXML(responseStream);
}
return null;
}
/**
* 解析返回xml数据
*
* @param responseStream
* @return
* @throws Exception
*/
private static String parseXML(InputStream responseStream) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(responseStream, "UTF-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_TAG:
if ("getMobileCodeInfoResult".equals(parser.getName())) {
return parser.nextText();
}
break;
}
event = parser.next();
}
return null;
}
}
[html] view
plaincopy
package cn.class3g.service;
…
public class MobileService {
public static String getMobileAddress(String mobile) throws Exception {
InputStream inStream = MobileService.class.getClassLoader()
.getResourceAsStream("mobilesoap.xml");
byte[] data = StreamTool.readInputStream(inStream);
String xml = new String(data);
String soap = xml.replaceAll("\\$mobile", mobile);
/**
* 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile
* 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;
*/
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
data = soap.getBytes();// 得到了xml的实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
"application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == 200) {
InputStream responseStream = conn.getInputStream();
return parseXML(responseStream);
}
return null;
}
/**
* 解析返回xml数据
*
* @param responseStream
* @return
* @throws Exception
*/
private static String parseXML(InputStream responseStream) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(responseStream, "UTF-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_TAG:
if ("getMobileCodeInfoResult".equals(parser.getName())) {
return parser.nextText();
}
break;
}
event = parser.next();
}
return null;
}
}
工具类StreamTool
view
plaincopy
to clipboard
package cn.class3g.utils;
…
public class StreamTool {
/**
* 从输入流读取数据
* @param inStream
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) !=-1 ){
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
}
Activity类MobileBelongActivity
package cn.class3g.mobile;
…
public class MobileBelongActivity extends Activity {
private static final String TAG = "MainActivity";
private EditText mobileText;
private TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mobileText = (EditText) this.findViewById(R.id.mobile);
resultView = (TextView) this.findViewById(R.id.result);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mobile = mobileText.getText().toString();
try {
String address = MobileService.getMobileAddress(mobile);
resultView.setText(address);
} catch (Exception e) {
Log.e(TAG, e.toString());
Toast.makeText(MobileBelongActivity.this, R.string.error, 1).show();
}
}
});
}
}
相关文章推荐
- 使用WIFI连接android进行调试和adb操作
- Android AIDL 必看内容
- Android开发实践:用脚本编译Android工程
- Android开发实践:用脚本编译Android工程
- Windows下获取Android系统源码
- LG推新机G Pro 2:硬件高端,创新略显不足
- 【安卓】android.view.ViewTreeObserver.OnGlobalLayoutListener
- Android手机一键Root原理分析
- Android中将Bitmap以数组形式保存到SD卡
- Android chromium 和 Android Webkit 区别
- 采用maven方式开发android
- Android系统框架
- Android的bitmap和优化
- Android 欢迎动画
- android向本地写入缓存
- Mono for Android or Android 中的事件怎么写!
- 简述安装android开发环境
- Android自定义标题栏
- Android:开发环境搭建、移植
- android canvas 绘制 Cirle 以外的区域为指定颜色