Http查询手机号码归属地

效果图
在这里插入图片描述效果图


布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 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">


    <EditText
        android:id="@+id/input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/button01"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- 右边对齐父亲的右边 -->
    <Button
        android:id="@+id/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="info"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button01"
        app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

函数操作

package com.example.wangluoqingqiu;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private EditText editText;
    private TextView textView;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    textView.setText( (String) msg.obj );
                    break;
                case 1:
                    textView.setText( (String) msg.obj );
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        findByID();
        dianjia();
    }

    private void dianjia() {
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread( new Runnable() {
                    @Override
                    public void run() {
                        requestData(editText.getText().toString());
                    }
                } ).start();
            }
        } );
    }

    //路径
    private String apiUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
    //点击获取数据
    private void requestData(String string) {
        try {
            URL url = new URL( apiUrl+string );
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout( 3000 );
            urlConnection.setReadTimeout( 3000 );
            if (urlConnection.getResponseCode()==200){
                String S = stream2String( urlConnection.getInputStream(), "gbk" );
                handler.sendMessage( handler.obtainMessage(0,S) );
            }else {
                handler.sendMessage( handler.obtainMessage(1,"错误") );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //点击设置数据
    private String stream2String(InputStream inputStream, String gbk) throws IOException {
        StringBuilder sb = new StringBuilder(  );
        BufferedReader br = new BufferedReader( new InputStreamReader( inputStream ,gbk) );
        for (String  tmp = br.readLine(); tmp!=null  ; tmp=br.readLine()) {
            sb.append( tmp );
        }
        return sb.toString();
    }

    private void findByID() {
        button = findViewById( R.id.button01 );
        editText = findViewById( R.id.input );
        textView = findViewById( R.id.textview );
    }
}


加网络权限

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

对了!老师使用的是将常量赋值给一个英文但是大家要知道是啥意思(最好不要使用魔法数字)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值