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.HttpRetryException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Executor;
public class MainActivity extends AppCompatActivity {
private Button query;
private TextView info;
private EditText input;
private final int UPDATE_PHONE_INFO = 0;
private final int GET_PHONE_INFO_ERROR = 1;
//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);
//寻找资源id
input = findViewById(R.id.input);
input.setText("18679906337");
query = findViewById(R.id.quey);
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 String apUrl="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
private void requestData(String phoneName) {
//做网络操作、必须在子线程中做
//查询手机号
try {
//请求地址
URL url = new URL(apUrl+phoneName);
//链接 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 = stream2String(urlConnection.getInputStream(),"gbk");
//展示
// info.setText(result);
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 (IOException e) {
e.printStackTrace();
}
}
private String stream2String(InputStream inputStream, String gbk) throws IOException{
StringBuilder stringBuilder = new StringBuilder();
InputStreamReader isr = new InputStreamReader(inputStream,gbk);
BufferedReader br = new BufferedReader(isr);
for (String tmp = br.readLine(); tmp!=null; tmp=br.readLine()){
stringBuilder.append(tmp);
}
return stringBuilder.toString();
}
}
加粗样式
<?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="呜啦啦啦"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/quey"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="@+id/quey"
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/info"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="信息!"
app:layout_constraintTop_toBottomOf="@id/quey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</android.support.constraint.ConstraintLayout>