您的位置:首页 > 其它

电话号码归属地查询

2014-11-25 16:48 190 查看
简单的电话号码归属地查询器

一、说明:仅供初学者借鉴,希望多多提意见,共同进步。

二、项目结构及界面图展示:





三、布局文件,较简单

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="电话号码归属地查询" />

<EditText

android:id="@+id/et_phoneNumber"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/textView1"

android:layout_marginTop="37dp"

android:ems="11"

android:hint="请输入电话号码:" >

</EditText>

<Button

android:id="@+id/but_search"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/et_phoneNumber"

android:layout_below="@+id/et_phoneNumber"

android:layout_marginTop="20dp"

android:onClick="searchPhone"

android:text="查询归属地" />

<TextView

android:id="@+id/tV_info"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/but_search"

android:layout_below="@+id/but_search"

android:layout_marginLeft="24dp"

android:layout_marginTop="76dp"

/>

</RelativeLayout>

四、代码:

package comcxrh.phonesearch;

import comcxrh.phonesearch.Utils.WebServiceUtils;

import android.os.AsyncTask;

import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.webkit.WebBackForwardList;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private EditText et_phoneNumber;

private TextView tv_info;

private String phoneNumber;

private String path;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

et_phoneNumber=(EditText) findViewById(R.id.et_phoneNumber);

tv_info=(TextView) findViewById(R.id.tV_info);

}

/**

*按钮的监听事件

* @param v

*/

public void searchPhone(View v){

phoneNumber=et_phoneNumber.getText().toString().trim();

path="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

if(phoneNumber!=null && !"".equals(phoneNumber)){

//查询号码

getInternetData();

}else{

Toast.makeText(this, "请输入电话号码", Toast.LENGTH_LONG).show();

}

}

/**

* 用异步任务的方法,查询号码

*/

private void getInternetData(){

new AsyncTask<Void, Void, String>(){

@Override

protected void onPreExecute() {

// TODO Auto-generated method stub

tv_info.setText("正在查询,请稍后......");

super.onPreExecute();

}

protected String doInBackground(Void... arg0) {

try {

return WebServiceUtils.queryPhone(path, phoneNumber);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

@Override

protected void onPostExecute(String result) {

// TODO Auto-generated method stub

tv_info.setText(result);

super.onPostExecute(result);

}

}.execute();

}

}

另一个工具类:

package comcxrh.phonesearch.Utils;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import comcxrh.phonesearch.MainActivity;

public class WebServiceUtils {

public static String queryPhone(String path,String phoneNumber) throws Exception{

String data=readXml().replace("phone", phoneNumber);

URL url=new URL(path);

HttpURLConnection conn=(HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setConnectTimeout(5000);

conn.setRequestProperty("Host", "webservice.webxml.com.cn");

conn.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");

conn.setRequestProperty("Content-Length", data.length()+"");

conn.setDoOutput(true);

conn.getOutputStream().write(data.getBytes());

int code=conn.getResponseCode();

if(code==200)

{

InputStream is=conn.getInputStream();

return getPhoneNumber(is);

}

return null;

}

private static String getPhoneNumber(InputStream is) throws Exception {

XmlPullParser parser=Xml.newPullParser();

parser.setInput(is, "utf-8");

for(int event=parser.getEventType();event!=XmlPullParser.END_DOCUMENT;event=parser.next())

{

if(event==XmlPullParser.START_TAG && parser.getName().equals("getMobileCodeInfoResult"))

{

return parser.nextText();

}

}

return null;

}

public static String readXml() throws IOException{

ByteArrayOutputStream bao=new ByteArrayOutputStream();

InputStream is=MainActivity.class.getClassLoader().getResourceAsStream("phonexml.xml");

byte[] buffer=new byte[1024];

int len=-1;

while((len=is.read(buffer))!=-1){

bao.write(buffer,0,len);

}

is.close();

bao.close();

return new String(bao.toByteArray());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: