网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android中怎么利用HttpURLConnection访问网络

Android中怎么利用HttpURLConnection访问网络,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

成都创新互联-专业网站定制、快速模板网站建设、高性价比驻马店网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式驻马店网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖驻马店地区。费用合理售后完善,十载实体公司更值得信赖。

一、 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访问网络问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


文章题目:Android中怎么利用HttpURLConnection访问网络
链接地址:http://bjjierui.cn/article/geijei.html

其他资讯