android链接网络的方法,Android 用HttpURLConnection访问网络的方法

本文详细介绍了Android中如何通过HttpURLConnection以GET和POST方式访问网络。在GET请求中,设置了连接和读取超时,并打印服务器响应。在POST请求中,不仅设置了超时,还写入了POST数据,并读取了响应。需要注意HTTP访问不能在主线程进行,通常在子线程处理。同时,文章提到了Android不同版本中HttpClient与HttpURLConnection的使用变化。
摘要由CSDN通过智能技术生成

一、 HttpURLConnection以GET方式访问网络:

HttpURLConnection connection = null;

try {

URL url = new URL("https://www.xxx.com/");

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");//设置访问方式为“GET”

connection.setConnectTimeout(8000);//设置连接服务器超时时间为8秒

connection.setReadTimeout(8000);//设置读取服务器数据超时时间为8秒

if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {

//从服务器获取响应并把响应数据转为字符串打印

InputStream in = connection.getInputStream();

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

StringBuilder response = new StringBuilder();

String line;

while (null != (line = reader.readLine())) {

response.append(line);

}

Log.d(TAG, response.toString());

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (null!= connection) {

connection.disconnect();

}

}

二、 HttpURLConnection以POST方式访问网络:

HttpURLConnection connection = null;

try{

URL url = new URL("https://www.xxx.com/");

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");

connection.setConnectTimeout(8000);

connection.setReadTimeout(8000);

connection.setDoOutput(true);// 使用 URL 连接进行输出

connection.setDoInput(true);// 使用 URL 连接进行输入

connection.setUseCaches(false);// 忽略缓存

// 建立输出流,并写入数据

OutputStream outputStream = connection.getOutputStream();

DataOutputStream dataOutputStream = new DataOutputStream(outputStream);

dataOutputStream.writeBytes("username=admin&password=888888");

dataOutputStream.close();

if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {

// 当正确响应时处理数据

StringBuffer response = new StringBuffer();

String line;

BufferedReader responseReader =

new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));

// 处理响应流,必须与服务器响应流输出的编码一致

while (null != (line = responseReader.readLine())) {

response.append(line);

}

responseReader.close();

Log.d(TAG, response.toString());

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (null!= connection) {

connection.disconnect();

}

}

注意:

1. HTTP访问是不允许在主线程进行的,否则会报错。因此上面的操作应该在新线程中进行。

2. 一般要用HttpURLConnection.getResponseCode() == 200来判断是否正常响应。为true则正常响应。

3. 在Android 2.2及以下版本,使用的是HttpClient,Android 2.3及以上版本,使用的是HttpURLConnection,而Android5.1之后废弃了HttpClient的相关Api。因此HttpClient用法不再进行研究。

4. 以POST方式提交数据时,每条数据要以键值对的方式提交,各条数据之间以&隔开。比如上面的代码中dataOutputStream.writeBytes(“username=admin&password=888888”);

5. 上面用到了StringBuilder和StringBuffer,没有什么特别用意,只是顺便用下。StringBuilder在单线程下比StringBuffer更高效,但不是线程安全的。

以上这篇Android 用HttpURLConnection访问网络的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值