获取手机号码信息用HTTP实现方法解析

时间: 作者: 点击量:
获取手机号码信息用HTTP实现方法解析。java实现手机号码获取类。
package com.example.day02;
 
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.TextView;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
public class MainActivity extends AppCompatActivity {

 

private EditText input;
private Button query;
private TextView info;
private final int UPDATE_PHONE_INFO=0;
private final int GET_PHONE_INFO_ERROR=1;
private Handler mHandler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case UPDATE_PHONE_INFO:
                info.setText((String) msg.obj);
                break;
            case GET_PHONE_INFO_ERROR:
                info.setText((String) msg.obj);
                break;
        }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取资源id
   input=findViewById(R.id.input);
   query =findViewById(R.id.query);
   info = findViewById(R.id.tv);
   query.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
            //线程
           new Thread(new Runnable() {
               @Override
               public void run() {
                   //切换到子线程中请求数据
                   requestData(input.getText().toString());
               }
           }).start();
       }
   });
}
private String apiUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
//重定向
private void requestData(String num){
    //网络操作,必须在子线程中做
    try {
        //请求地址
        URL url = new URL(apiUrl + num);
        //连接   ctrl+h查看继承结构
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        //请求方法
        urlConnection.setRequestMethod("GET");
        //连接超时时间
        urlConnection.setConnectTimeout(5000);
        //读取超时时间
        urlConnection.setReadTimeout(5000);
        //获取返回结果 :状态码
        int responseCode = urlConnection.getResponseCode();
        if(responseCode==200){
            //请求成功
            //获取结果
            String result = stream(urlConnection.getInputStream(), "gbk");
            //ctrl+q查看文档
            Message message = mHandler.obtainMessage(UPDATE_PHONE_INFO, result);
            mHandler.sendMessage(message);
        }else {
            mHandler.sendMessage(mHandler.obtainMessage(GET_PHONE_INFO_ERROR,"请求返回错误"+responseCode));
        }
        //断点
        urlConnection.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
private String stream(InputStream inputStream, String gbk) throws Exception {
    StringBuilder stringBuilder = new StringBuilder();
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, gbk);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    for (String tmp=bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){
        stringBuilder.append(tmp);
    }
    return stringBuilder.toString();
}
 

}

上一篇:访客手机号码获取,大数据大未来。定向精准客户挖掘

下一篇:过滤检测手机号码是否开通微信,日跑30万检测手机号开通微信

注册体验: