package com.example.day2;
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.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.Executor;
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;
//thread
//ctrl + p 查看方法需要参数
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message 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);
input = findViewById(R.id.input);
input.setText("18679906337");
//快速提取变量
//ctrl + alt + v 局部
//ctrl + alt + f 全局
query = findViewById(R.id.query);//shift + f6 快捷重构
info = findViewById(R.id.info);
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 void s() {
//线程
//Thread;
//线程池
//Executor;
//new Thread().run();
new Thread(){
@Override
public void run() {
super.run();
requestData("");
}
}
//开启 执行
.start();
}
private String apiUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
//重定向
private void requestData(String phoneNum) {
//做网络操作,必须在子线程中做
//查询手机号
//java对http协议的简单封装 HttpUrlConnection
try {
//请求地址
URL url = new URL(apiUrl + phoneNum);
//连接 ctrl + h 查看继承结构
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();//
//请求方法
urlConnection.setRequestMethod("GET");
//连接超时时间
urlConnection.setConnectTimeout(5000);
//读取超时时间
urlConnection.setReadTimeout(5000);
//获取返回结果: 状态码
//响应状态码 (anr) NetworkOnMainThreadException
int responseCode = urlConnection.getResponseCode();
if(responseCode == 200) {//请求成功
//获取结果
String result = stream2String(urlConnection.getInputStream(), "gbk");
//展示
//info.setText(result);
//ctrl + q 查看文档
Message message = mHandler.obtainMessage(UPDATE_PHONE_INFO, result);
mHandler.sendMessage(message);
} else {
//info.setText("请求返回错误: " + responseCode);
mHandler.sendMessage(mHandler.obtainMessage(GET_PHONE_INFO_ERROR, "请求返回错误: " + responseCode));
}
urlConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String stream2String(InputStream inputStream, String charsetName) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
InputStreamReader isr = new InputStreamReader(inputStream, charsetName);
BufferedReader br = new BufferedReader(isr);
for (String tmp = br.readLine(); tmp != null; tmp = br.readLine()) {
stringBuilder.append(tmp);
}
return stringBuilder.toString();
}
}