try {
URL url = new URL("请求地址");
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
//设置请求方式
openConnection.setRequestMethod("GET");
//得到请求码
int responseCode = openConnection.getResponseCode();
if (responseCode == 200)
{
InputStream inputStream = openConnection.getInputStream();
int len;
byte[] b = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(b)) != -1)
{
arrayOutputStream.write(b, 0, len);
}
String strJson = arrayOutputStream.toString();
//handler发送信息,主线程中更新数据
handler.obtainMessage(SUCCESS, strJson).sendToTarget();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpURLConnection请求
最新推荐文章于 2023-06-07 22:11:30 发布