Android 输入手机号码(GET网络请求)查询手机号码归属地

Android 输入手机号码(GET网络请求)查询手机号码归属地

首先在xml文件上界面布局
采用LinearLayout来布局界面,并添加一个按钮点击来进行获取信息,在添加一个EditText来输入我们要进行查询的手机号码,最后添加一个TextView控件来显示手机归属地信息

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="wrap_content"
        android:text="号码归属地查询"
        android:textSize="30sp"
        android:padding="20px"
        android:gravity="center"/>
    <EditText
        android:id="@+id/e1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20px"
        android:layout_marginTop="20px"
        android:inputType="phone"//只允许输入数字
        android:hint="请输入手机号码"/>//提示输入
    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30px"
        android:text="点击获取"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textColor="#E9749C"
        android:layout_marginTop="20dp"
        android:text="手机号码信息:"/>
    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text=""
        android:padding="10dp"
        android:textSize="25sp"/>

</LinearLayout>

界面截图

在这里插入图片描述
第二在Mainactivity.java完成功能的实现
第一步:绑定id
在这里插入图片描述
第二步:按钮添加监听事件
在这里插入图片描述
第三步:完成网络的请求
在网络请求之前在AndroidManifest.xml文件中添加网络权限

<uses-permission android:name="android.permission.INTERNET"/>

定义一个请求接口

private void  requestDate()
{
    
    
}

在该接口进行网络请求

    private void requestDate() {
    
    
        final String phone = e1.getText().toString().trim();//获取手机号码
        String url = "http://api.k780.com/?app=phone.get&phone=" + phone + "&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";//请求数据
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
    
    
            @Override
            public void onFailure(Call call, IOException e) {
    
    

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
    
    
                String result = response.body().string();
                Gson gson = new Gson();
                final DateModel dateModel = gson.fromJson(result, DateModel.class);
                runOnUiThread(new Runnable() {
    
    
                    @Override
                    public void run() {
    
    
                        refreshDate(dateModel);
                    }
                });
            }
        });
    }

第四步:提取解析后的相关数据

    private void refreshDate(DateModel dateModel)
    {
    
    
        if(dateModel==null||dateModel.getResult()==null)
        {
    
    
            return;
        }
        DateModel.ResultBean resultBean=dateModel.getResult();
        t1.setText(resultBean.getPhone()+"\n"+resultBean.getAtt().replace(","," ")+"\n"+resultBean.getCtype());
    }

最后一步:数据解析
定义一个类DataModel.class
public class DataModel implements Serializable {

}
复制链接请求的json全部数据
在这里插入图片描述
使用快捷键(Alt+s)粘贴全部过去数据,之后一直点击OK
在这里插入图片描述
如果需要在真机使用软件,请在(AndroidManifest.xml)中的application输入以下代码,防止真机对网络请求不了

android:usesCleartextTraffic="true"

下面是本项目的源代码
https://download.csdn.net/download/Scxioi0/12910259

猜你喜欢

转载自blog.csdn.net/Scxioi0/article/details/108908419