package com.bw.a616day01;
import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;
import android.os.AsyncTask;
import android.util.Log;
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;
public class MyTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(3000);
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.connect();
if (httpURLConnection.getResponseCode()==200) {
InputStream inputStream = httpURLConnection.getInputStream();
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line="";
while ((line = reader.readLine()) != null){
builder.append(line);
Log.i(TAG, "doInBackground: "+builder);
}
return builder.toString();
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
}
get请求
最新推荐文章于 2024-07-18 11:07:23 发布