发送XML
通过URL封装路径打开一个HttpURLConnection
设置请求方式,Content-Type和Content-Length
XML文件的Content-Type为:text/xml;charset=UTF-8
使用HttpURLConnection获取输出流输出数据
WebService
WebService是发布在网络上的API,可以通过发送XML调用,WebService返回结果也是XML数据
WebService没有语言限制,只要可以发送XML数据和接收XML数据即可
http://www.webxml.com.cn网站上提供了一些WebService服务,我们可以对其进行调用
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo中提供了电话归属地查询的使用说明
内容如下:
SOAP 1.2
以下是 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>
HTTP GET
以下是 HTTP GET 请求和响应示例。所显示的占位符需替换为实际值。
GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string HTTP/1.1
Host: webservice.webxml.com.cn
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>
HTTP POST
以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。
POST /WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length
mobileCode=string&userID=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>
下面为具体实例及代码:
界面显示:
向WebService发送的XML文件: send.xml(放置在SRC路径下)
<?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>$number</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>
布局文件man.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/phoneET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" >
<requestFocus />
</EditText>
<Button
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="归属地查询" />
<TextView
android:id="@+id/locationTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
/>
</LinearLayout>
功能清单文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.webservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.itheima.webservice"
/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
<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>
</manifest>
MainActivity:
package com.itheima.webservice;
import com.itheima.webservice.service.NumberService;
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 phoneET;
private TextView locationTV;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
phoneET = (EditText) findViewById(R.id.phoneET);
locationTV = (TextView) findViewById(R.id.locationTV);
}
public void onClick(View view){
try {
NumberService service = new NumberService();
String num = phoneET.getText().toString();
String loacation = service.getLocation(num);
locationTV.setText(loacation);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "查无此号", 1).show();
}
}
}
NumberService:
package com.itheima.webservice.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
import com.itheima.webservice.util.StreamUtil;
public class NumberService {
public String getLocation(String number) throws Exception {
// 读取本地准备好的文件, 用输入的号码替换原来的占位符
InputStream in = NumberService.class.getClassLoader().getResourceAsStream("send.xml");
byte[] data = StreamUtil.load(in);
String content = new String(data);
content = content.replace("$number", number);
// 创建连接对象, 设置请求头, 按照Webservice服务端提供的要求来设置
URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestProperty("Host", "webservice.webxml.com.cn");
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", content.getBytes().length + "");
conn.setRequestMethod("POST");
// 输出数据
conn.setDoOutput(true);
conn.getOutputStream().write(content.getBytes());
// // 获取服务端传回的数据, 解析XML, 得到结果
XmlPullParser parser = Xml.newPullParser();
parser.setInput(conn.getInputStream(), "UTF-8");
for (int type = parser.getEventType();type!=XmlPullParser.END_DOCUMENT;type=parser.next())
if(type==XmlPullParser.START_TAG&&parser.getName().equals("getMobileCodeInfoResult")){
return parser.nextText();
}
return "没有找到此号码";
}
}
读取输入流工具类StreamUtil:
package com.itheima.webservice.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamUtil {
public static byte[] load(InputStream in ) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte b [] = new byte[1024];
int len = -1;
while((len=in.read(b))!=-1){
baos.write(b,0,len);
}
baos.close();
return baos.toByteArray();
}
}
分享到:
相关推荐
Android中调用Webservice实现号码归属地查询案例!
android基于webservice的手机号码查询功能,自己学习的时候写的小例子,简单易懂!android基于webservice的手机号码查询功能,自己学习的时候写的小例子,简单易懂!
在android端使用了soap协议来调用网上的webservice接口,来获得手机号码的归属地信息
C# 调用WebService查询手机号码归属地
CSharp调用WebService实现手机号码归属地查询
我第一次使用webservice传输数据的时候用来参考的例子,里面代码很清晰,适合做参考,而且实现了号码归属地的查询,代码简洁。
Android调用webservice查询手机归属地信息,可以直接运行,非常的简单好用
Android访问webservice
Android平台之Webservice详解(超详细),Android平台之Webservice详解(超详细),Android平台之Webservice详解(超详细)
Android通过IIS搭建WebService访问 SQLServer,实现增、删、查
在Android中访问WebService接口
android webserviceandroid webserviceandroid webservice
本程序代码是利用HTTP协议发送XML数据并调用webservice(soap),来实现手机号码的查询,是一个简单的应用程序,适合Android开发程序员。
android调用.net的webservice的例子获取手机的归属地
自动查询手机号码归属地程序,用C#编写,程序通过soap调用Webservice查询,数据库中已经有130-189号段的归属看信息。运行时记得还原数据库哦
Android开发之WebService介绍
qt5.12.0 和 webservice 的demo,通过http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 查询手机号码归属, 简单有效
android 调用.net webservice
delphi XE5 ANDROID平台 调用 webservice并访问操作MSSQL数据库
比较适合android 刚入门的小白,需要调用webService,此demo能迅速带你入门