Android学习笔记(十二)-WebService调用

在J2EE项目中,我们可以根据WSDL通过各种框架来直接生成WebService的客户端调用代码,在Android中,也有第三方的框架可用于调用WebService,这里介绍一种通过使用HTTP协议来模拟WebService调用过程的方法,即自己根据规范来构建请求的XML,然后通过HTTP协议发送,最后自己解析响应的XML。

这里以一个获得国内手机号码归属地数据库信息的WebService为例:

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx

SSOAP 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>


首先是要构建请求的XML,可以采用Pull方式来构建,这里采用正则表达式的方式来替换字符串,这样更简单一些。

在src下建立一个mobilesoap.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>

使用正则表达式将$mobile替换成实际的手机号即可。

至于返回的XML可以使用Pull方式来解析。

项目运行效果如下:



布局文件:layout/main.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" >

    <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" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

    <TextView
        android:id="@+id/address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

数据文件:values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MobileAddressActivity!</string>
    <string name="app_name">手机号归属地查询</string>
    <string name="mobile">手机号</string>
    <string name="button">查询</string>
</resources>

MobileAddressActivity.java

package com.android.ws;

import java.io.InputStream;

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;

import com.android.service.MobileService;

public class MobileAddressActivity extends Activity {
	private EditText mobileText;
    private TextView addressView;
    private static final String TAG = "MainActivity";
    /** 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);
        addressView = (TextView)this.findViewById(R.id.address);
        Button button = (Button)this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				String mobile = mobileText.getText().toString();
				InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");
				try {
					addressView.setText(MobileService.getMobileAddress(inStream, mobile));
				} catch (Exception e) {
					Log.e(TAG, e.toString());
					e.printStackTrace();
					Toast.makeText(MobileAddressActivity.this, "查询失败", 1).show();
				}
			}
		});
    }
}

MobileService.java

package com.android.service;

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 android.util.Xml;

import com.android.utils.StreamTool;

public class MobileService {
	private static String readSoapFile(InputStream inStream, String mobile) throws Exception{
		byte[] data = StreamTool.readInputStream(inStream);
		String soapxml = new String(data);
		Map<String, String> params = new HashMap<String, String>();
		params.put("mobile", mobile);
		return replace(soapxml, params);
	}

	
	public 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.setRequestMethod("POST");
		conn.setConnectTimeout(5 * 1000);
		conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据
		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){
			return parseResponseXML(conn.getInputStream());
		}
		return null;
	}
	
	private static String parseResponseXML(InputStream inStream) throws Exception{
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "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;
	}

}

StreamTool.java

package com.android.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {

	/**
	 * 从输入流中读取数据
	 * @param is 输入流
	 * @return
	 * @throws Exception
	 */
	public static byte[] readInputStream(InputStream is) throws Exception{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = is.read(buf)) != -1){
			baos.write(buf, 0, len);
		}
		baos.flush();
		baos.close();
		
		return baos.toByteArray();
	}
}

另外需要在AndroidManifest.xml中添加网络访问权限:

<!-- 访问网络的权限 -->
<uses-permission android:name="android.permission.INTERNET"/>

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值