转载自原文http://www.oschina.net/question/565065_67907
发送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();
- }
- }