查询手机号码

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

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

/**
 * 查询手机号归属地
 */
public class MainActivity extends AppCompatActivity {

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.edit_text);

        //要求输入电话号码...十一位的时候去访问网络查询
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            //输入文本改变的时候
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //判断是否是十一位
                String s = charSequence.toString();
                if (s.length() == 11){
                    //查询 ...访问网络
                    searchPhone(s);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    /**
     * 查询手机号
     * @param s 手机号
     */
    private void searchPhone(final String s) {
        new  Thread(){
            @Override
            public void run() {
                //路径
                String path = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+s;

                try {
                    URL url = new URL(path);

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

                    //设置
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(5000);
                    connection.setConnectTimeout(5000);

                    //获取
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200){
                        //返回的字节流
                        InputStream inputStream = connection.getInputStream();

                        //把字节流转成字符串
                        String string = streamToString(inputStream,"gbk");



                        String json = string.substring(string.indexOf("{"));

                        Log.i("string",json);

                        //json解析
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }

    /**
     * __GetZoneResult_ = {    mts:'1571531',    province:'山东',    catName:'中国移动',    telString:'15715317583',    areaVid:'30501',    ispVid:'3236139',    carrier:'山东移动'}
     */

    /**
     * 把流转换成字符串
     * @param inputStream
     * charset  编码格式
     * @return
     */
    private String streamToString(InputStream inputStream,String charset) {

        //转成字符流
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);

            //转成缓冲流
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            //读取
            String s = null;
            StringBuilder builder = new StringBuilder();

            while ((s = bufferedReader.readLine()) != null){
                builder.append(s);
            }

            bufferedReader.close();

            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }
}

发布了8 篇原创文章 · 获赞 0 · 访问量 2347
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览