HTTPURLConnection
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(v->{
getHTTPRes();
});
}
private void getHTTPRes(){
new Thread(()->{
HttpURLConnection connection=null;
try {
URL url=new URL("https://cn.bing.com/");
connection= (HttpURLConnection) url.openConnection();
//设置连接的请求方法和延迟时限
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
//获取输入流
InputStream inputStream = connection.getInputStream();
//繁琐的IO操作