public class MainActivity extends AppCompatActivity {

private Button btn_go;
private ListView list_view;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_go = findViewById(R.id.btn_go);
list_view = findViewById(R.id.list_view);
btn_go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//执行异步任务
MyTask myTask = new MyTask();
myTask.execute("https://www.toutiao.com/api/pc/focus/");
}
});

}
class MyTask extends AsyncTask<String,Void,String>{

@Override
protected String doInBackground(String... strings) {

StringBuilder builder = new StringBuilder();
//1.创建url
try {
URL url = new URL(strings[0]);

//2.打開连接
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
//3.设置
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
//4.判断响应码
if (urlConnection.getResponseCode() ==200){
//5.获取数据
InputStream inputStream = urlConnection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

String str;
while ((str = reader.readLine())!= null){

builder.append(str);
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
//在进行网络请求得到数据之后,要操作的事件,,解析,更新Ui


@Override
protected void onPostExecute(String s) {
// s:代表doInBackground 返回的数据
Gson gson = new Gson();
Result_bean result = gson.fromJson(s, Result_bean.class);
List<Result_bean.DataBean.PcFeedFocusBean> list = result.getData().getPc_feed_focus();

//更新UI的代码
MyAdapter adapter = new MyAdapter (list,MainActivity.this);
list_view.setAdapter(adapter);

}
}
}