訪問webservice獲取手機號碼歸屬地


Ÿ   http://www.webxml.com.cn 網站上提供了一些WebService服務,我們可以對其進行調用


package cs.utils;

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

public class StreamTools {
	
	public static byte[] read(InputStream inStream) throws IOException{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte [] buffer = new byte[1024];
		int len = 0;
		while((len=inStream.read(buffer))!=-1){
			outStream.write(buffer);
		}
		inStream.close();
		outStream.close();
		
		return outStream.toByteArray();
	}
}

package cs.service;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;
import cs.utils.StreamTools;

public class QueryService {

	public String query(String num) throws IOException, XmlPullParserException{
		InputStream in = QueryService.class.getClassLoader().getResourceAsStream("template.xml");
		String data = new String(StreamTools.read(in));
		in.close();
		data = data.replace("phoneNum", num);
		
		URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(3000);
		conn.setRequestMethod("POST");
		
		conn.setRequestProperty("HOST", "webservice.webxml.com.cn");
		conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		conn.setRequestProperty("Content-Length", data.getBytes().length+"");
		conn.setDoOutput(true);
		conn.getOutputStream().write(data.getBytes());
		
		int code = conn.getResponseCode();
		if(code == 200){
			return paresAddaress(conn.getInputStream());
		}
		return null;
	}

	private String paresAddaress(InputStream inputStream) throws XmlPullParserException, IOException {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream,"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 null;
	}
}

package com.example.phonenumber;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;
import cs.service.QueryService;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText number;
	private TextView show;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		if(android.os.Build.VERSION.SDK_INT>9){
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		number = (EditText) findViewById(R.id.numberphone);
		show = (TextView) findViewById(R.id.show);
		
	}
	public void query(View v){
		switch (v.getId()) {
		case R.id.ok:
            QueryService qs = new QueryService();
            String address = null;
            String numberphone = number.getText().toString().trim();
            try {
				address = qs.query(numberphone);
				show.setText(address);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Toast.makeText(getApplicationContext(), "出錯了", 1).show();
				e.printStackTrace();
			}
			break;

		default:
			break;
		}
	}



}



發佈了22 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章