android+号码归属地数据库,Android手机号码归属地的查询「建议收藏」

android+号码归属地数据库,Android手机号码归属地的查询「建议收藏」一个简单的Demo,从聚合数据申请手机号码归属地数据接口;在EditText中输入待查询号码,获取号码后在子线程中使用HttpUrlconnection获取JSON数据,之后进行解析;数据获取完成后,在主线程中更新UI,显示获取的号码归属地信息。布局文件android:layout_width=”match_parent”android:layout_height=”match_parent”an…

大家好,又见面了,我是你们的朋友全栈君。

一个简单的Demo,从聚合数据申请手机号码归属地数据接口;

在EditText中输入待查询号码,获取号码后在子线程中使用HttpUrlconnection获取JSON数据,之后进行解析;

数据获取完成后,在主线程中更新UI,显示获取的号码归属地信息。

布局文件

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:orientation=”vertical” >

android:id=”@+id/et_querylocation”

android:layout_height=”wrap_content”

android:layout_width=”match_parent”

android:textColor=”#000000″

android:hint=”输入号码”/>

android:onClick=”query”

android:textSize=”24sp”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”查询”/>

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:id=”@+id/tv_phonelocation”

android:textSize=”20sp”

android:textColor=”#000000″/>

java代码

package com.example.phonehome;

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.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;

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;

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 =”申请的APP KEY”;

//子线程中查询数据开始、完成时发送消息,完成相应操作

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 + ” ” + company + ” ” + card);

break;

default:

break;

}

};

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

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”);

} 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 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);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163155.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 现在在人工智能发展方面已经有了一些泡沫_人工智能毁灭人类

    现在在人工智能发展方面已经有了一些泡沫_人工智能毁灭人类本文3560字,建议阅读9分钟。本文对人工智能在2020年的发展态势进行分析。对付泡沫的有效方法,就是用另一个更大的泡沫包住它,这也是常说的「嵌套式泡沫」。如果要追求可持续繁荣,必须要同时做到两件事:小心地把里面的虚假消掉、认真地把外面的空间做大。身处人工智能巨轮上的我们,越来越频繁地听人谈论起人工智能过快增长而导致泡沫丛生,独角兽越来越多,但跑出资本怪圈独立生长的却寥寥…

    2022年9月30日
    1
  • Oracle数据库ORA-12154: TNS: 无法解析指定的连接标识符解决方法[通俗易懂]

    Oracle数据库ORA-12154: TNS: 无法解析指定的连接标识符解决方法[通俗易懂]对于这个问题,对于我这种初学者来说是经常遇到的,今天就把可靠的解决发法记于此,希望能帮助到大家。ORA-12154:TNS:无法解析指定的连接标识符第一步:查看自己的Oracle服务是否打开。OracleDBConsoleORCL是Oracle网页端管理工具的服务,访问地址一般为“http://127.0.0.1:1158/em/console/logon/logon”,如果不习惯用…

    2022年7月19日
    13
  • vue纯前端分页_基于vue的表格组件

    vue纯前端分页_基于vue的表格组件vue分页组件(比上一版本好看一些),贴代码vue-page.js代码如下varvuePage={ template:’<divclass="page-bar"id="pager">\ <spanclass="form-inline">\ <selectclass="form-control"v-model=&a

    2022年10月1日
    0
  • 视频编解码基本流程

    视频编解码基本流程视频编解码基本框架

    2022年7月13日
    13
  • AngularJS总结

    AngularJS总结因为最近想学习一下ionic框架,了解到ionic是基于AngularJS语法,并且通过SASS构建应用程序,之前自己一直用Vue框架,还有Less,刚刚好趁此机会,学习一下AngularJS与SAS…

    2022年7月25日
    9
  • PyCharm设置护眼背景色

    PyCharm设置护眼背景色.方法一:      File->Seting->Editor-Colors->General->Text->Defaulttext->BackGround设置为E1F4E4

    2022年8月25日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号