调用webservice手机归属地查询

    有一种api是发布在网络上边,这就是webservice,android开发中类似手机归属地查询,天气查询等功能。详情参考网址:http://www.webxml.com.cn/.这里面有各种webservice服务。

  手机归属地查询是通过soap协议进行的。android客户端发送xml数据到webservice上边,其会返回一个同样为xml数据格式的数据给你,其实这返回的xml数据就是为soap协议。

  从webservice获取手机归属地代码AdressService:

package com.example.adress;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.example.utils.streamTool;

public class AdressService {

	public static String getAdress(String mobile) throws Exception{
		InputStream in = AdressService.class.getClassLoader().getResourceAsStream("soap12.xml");
		byte[] data = streamTool.read(in);
		String soap = new String(data,"UTF-8");
		soap = soap.replaceAll("\\$mobile", mobile);
		System.out.println("soap="+soap);
		byte[] enenty = soap.getBytes();
		String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
		HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		conn.setRequestProperty("Content-Length", String.valueOf(enenty.length));
		conn.getOutputStream().write(enenty);//将数据写出去
		if(conn.getResponseCode() == 200){
			System.out.println("请求成功");
			return XmlParse(conn.getInputStream());
		}
		return null;
	}

	private static String XmlParse(InputStream inXml) throws Exception{
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(inXml,"UTF-8");
		int event = pullParser.getEventType();
		while(event != XmlPullParser.END_DOCUMENT){
			switch (event) {
			case XmlPullParser.START_TAG:
				if("getMobileCodeInfoResult".equals(pullParser.getName())){//返回的协议
					System.out.println("tdw");
					return pullParser.nextText();
				}
				break;
			}
			event = pullParser.next();
		}
		return null;
	}
}

MainActivity文件:

package com.example.mobileadressquery;

import com.example.adress.AdressService;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	private EditText mobile_et;
	private TextView address_tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mobile_et = (EditText) this.findViewById(R.id.mobile);
		address_tv = (TextView) this.findViewById(R.id.adress);
	}
	
	public void query(View v){
		String mobile = mobile_et.getText().toString();
		try {
			String address = AdressService.getAdress(mobile);
			address_tv.setText(address);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="手机号" />
    
    
    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mobile"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        android:onClick="query"
        />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/adress"
        />
</LinearLayout>


代码链接: http://download.csdn.net/detail/tan313/8393363


你可能感兴趣的:(调用webservice手机归属地查询)