package com.iquiry;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_phone;
private TextView tv_phone,tv_phone2,tv_phone3,tv_phone4,tv_phone5;
private final static int START = 0;
private final static int FINISH = 1;
private String phone;//待查询号码
//号码信息
private static String province;
private static String city;
private static String company;
private static String card;
private static String areacode;
public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static final String APPKEY ="4e84bea91fbd38473c28a5ac2bc16be7";
//子线程中查询数据开始、完成时发送消息,完成相应操作
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case START:
Toast.makeText(MainActivity.this, "正在查询,请稍候", Toast.LENGTH_SHORT).show();
break;
case FINISH:
//在Textview中显示查得的号码信息(子线程中不能更新UI)
/*tv_phone.setText("归属省份:"+province+" "
+"归属城市:"+city+" "
+"城市区号:"+areacode+" "
+"邮政编码:"+zip+" "
+"运营商:"+company+" "
+"卡类型:"+card);*/
tv_phone.setText("归属省份:"+province);
tv_phone2.setText("归属城市:"+city);
tv_phone3.setText("城市区号:"+areacode);
tv_phone4.setText("运营商:"+company);
tv_phone5.setText("卡类型:"+card);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
//Button的点击事件,获取待查询号码后在子线程中进行查询
public void query(View v){
phone = et_phone.getText().toString().trim();
if (!TextUtils.isEmpty(phone)) {
new Thread(){
public void run() {
//开始查询
handler.obtainMessage(START).sendToTarget();
getRequest(phone);
//查得结果
handler.obtainMessage(FINISH).sendToTarget();
};
}.start();
}else {
Toast.makeText(MainActivity.this, "输入号码不能为空", Toast.LENGTH_SHORT).show();
}
}
//手机归属地查询
public static void getRequest(String phone){
String result =null;
String url ="http://apis.juhe.cn/mobile/get";//请求接口地址
Map params = new HashMap();//请求参数
params.put("phone",phone);//需要查询的手机号码或手机号码前7位
params.put("key",APPKEY);//应用APPKEY(应用详细页查询)
params.put("dtype","");//返回数据的格式,xml或json,默认json
try {
//得到JSON数据,并进行解析
result =net(url, params, "GET");
JSONObject object = new JSONObject(result);
JSONObject ob = new JSONObject(object.get("result").toString()+"");
province = ob.getString("province");
city = ob.getString("city");
company = ob.getString("company");
card = ob.getString("card");
areacode=ob.getString("areacode");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param strUrl 请求地址
* @param params 请求参数
* @param method 请求方法
* @return 网络请求字符串
* @throws Exception
*/
public static String net(String strUrl, Map params,String method) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
StringBuffer sb = new StringBuffer();
if(method==null || method.equals("GET")){
strUrl = strUrl+"?"+urlencode(params);
}
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
if(method==null || method.equals("GET")){
conn.setRequestMethod("GET");
}else{
conn.setRequestMethod("POST");
conn.setDoOutput(true);
}
//conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();
if (params!= null && method.equals("POST")) {
try {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(urlencode(params));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}
//将map型转为请求参数型
public static String urlencode(Map<String,String> data) {
StringBuilder sb = new StringBuilder();
for (Map.Entry i : data.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private void initView() {
setContentView(R.layout.activity_main);
et_phone = (EditText) findViewById(R.id.et_querylocation);
tv_phone = (TextView) findViewById(R.id.tv_phonelocation);
tv_phone2 = (TextView) findViewById(R.id.tv_phonelocation2);
tv_phone3 = (TextView) findViewById(R.id.tv_phonelocation3);
tv_phone4 = (TextView) findViewById(R.id.tv_phonelocation4);
tv_phone5 = (TextView) findViewById(R.id.tv_phonelocation5);
}
}
xml
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="手 机 归 属 地"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_gravity="center"
android:gravity="center"
android:background="#6699ff"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<EditText
android:id="@+id/et_querylocation"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border"
android:hint="请输入手机号"
android:gravity="center"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="@drawable/background_shape"
android:onClick="query"
android:text="查 询"
android:textColor="#6699ff"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_phonelocation"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:textColor="#000000"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="归属省份:"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_phonelocation2"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:textColor="#000000"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="归属城市:"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_phonelocation3"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:textColor="#000000"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="城市区号:"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_phonelocation4"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:textColor="#000000"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="运营商:"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_phonelocation5"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:textColor="#000000"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="卡类型"/>
</LinearLayout>