电话归属地查询的实现

原理图:

电话归属地查询的实现_第1张图片


1、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="电话号码" />
    <EditText android:id="@+id/et_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="query"
        android:text="查询" />
    <TextView android:id="@+id/tv_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="归属地信息" />

</LinearLayout>

2、MainActivity

package com.njupt.addressquery1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private EditText et_number;
	private TextView tv_info;
	private AddressQueryService service;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    
		et_number = (EditText) findViewById(R.id.et_number);
		tv_info = (TextView) findViewById(R.id.tv_info);
		service = new AddressQueryService();
		
		
	}

	public void query(View v){
		String number = et_number.getText().toString();
		
		try{
			String info = service.query(number);
			info = "黄俊东是一个大帅哥!!!\n章泽天是我的女神!!!!\n"+info;
			tv_info.setText(info);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

3、soap12.xml

请求的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>string</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

4、AddressQueryService

package com.njupt.addressquery1;

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

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class AddressQueryService {

	public String query(String number) throws Exception{
		String path =  "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5000);
		
		String soap = getXml();
		String xml = soap.replace("string", number);
		byte[] entity = xml.getBytes();
		
		conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		conn.setRequestProperty("Content-Length", entity.length + "");
		
		
		conn.setDoOutput(true);
		OutputStream os = conn.getOutputStream();
		os.write(entity);
		
		if(conn.getResponseCode() == 200){
			InputStream is = conn.getInputStream();
		
		    return parserXml(is);
		}
		
		return null;
	}
	
	public String getXml() throws Exception{
		InputStream is = getClass().getClassLoader().getResourceAsStream("soap12.xml");//加载XML文件
		//将流-----》字符串
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len = is.read(buffer)) != -1){
			bos.write(buffer,0,len);
		}
		
		String xml = bos.toString();
		
		is.close();
		bos.close();
		return xml;
	}
	
	public String parserXml(InputStream is) throws Exception{//解析XML
		XmlPullParser parser = Xml.newPullParser();
	    
		parser.setInput(is,"utf-8");
		
		int eventType = parser.getEventType();
		
		while(eventType != XmlPullParser.END_DOCUMENT){
			switch (eventType) {
			case XmlPullParser.START_TAG:
				if("getMobileCodeInfoResult".equals(parser.getName())){
					return parser.nextText();
				}
				break;

			default:
				break;
			}
			
			eventType = parser.next();
		}
		
		return null;
		
	}
}


你可能感兴趣的:(电话归属地查询的实现)