android底层的二种网络请求方式

1.HttpURLConnection
java.net包中的HttpURLConnection

2. HttpGet,HttpPost
org.apache.http包中的HttpGet和HttpPost类

HttpURLConnection

Get方法

 public static void requestByGet() throws Exception {
     String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
     // 新建一个URL工具
     URL url = new URL(path);
     // 打开一个HttpURLConnection连接
     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
     // 设置超时时间
     urlConn.setConnectTimeout(5 * 1000);
     // 发起连接
     urlConn.connect();
     // 判断请求状态 200成功
     if (urlConn.getResponseCode() == HTTP_200) {
         // 获取返回的数据
         byte[] data = readStream(urlConn.getInputStream());
         Log.i(TAG_GET, "Get方法要求乐成,返回数据如下:");
         Log.i(TAG_GET, new String(data, "UTF-8"));
     } else {
         Log.i(TAG_GET, "Get方法要求掉败");
     }
     // 关闭
     urlConn.disconnect();

 }


Post方法

 public static void requestByPost() throws Throwable {
     String path = "https://reg.163.com/logins.jsp";
     // 要求的参数转换为byte数组
     String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")+ "&pwd=" + URLEncoder.encode("android", "UTF-8");
     byte[] postData = params.getBytes();
     // 新建一个URL工具
     URL url = new URL(path);
     // 打开一个HttpURLConnection毗连
     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
     // 设置超时时间
     urlConn.setConnectTimeout(5 * 1000);
     // Post要求必需设置许可输出
     urlConn.setDoOutput(true);
     // Post要求不克不及应用缓存
     urlConn.setUseCaches(false);
     // 设置为Post要求
     urlConn.setRequestMethod("POST");
     urlConn.setInstanceFollowRedirects(true);
     // 设置要求Content-Type
     urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencode");
     // 发起连接
     urlConn.connect();
     // 发送要求参数
     DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
     dos.write(postData);
     dos.flush();
     dos.close();
     // 连接是否成功
     if (urlConn.getResponseCode() == HTTP_200) {
         // 获取返回的数据
         byte[] data = readStream(urlConn.getInputStream());
         Log.i(TAG_POST, "Post要求方法乐成,返回数据如下:");
         Log.i(TAG_POST, new String(data, "UTF-8"));
     } else {
         Log.i(TAG_POST, "Post方法要求掉败");

     }
 }

HttpGet HttpPost

HttpGet方法

 public static void requestByHttpGet() throws Exception {
     String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
     // 新建HttpGet工具
     HttpGet httpGet = new HttpGet(path);
     // 获取HttpClient工具
     HttpClient httpClient = new DefaultHttpClient();
     // 获取HttpResponse实例
     HttpResponse httpResp = httpClient.execute(httpGet);
     // 判断连接是否成功
     if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
         // 获取返回的数据
         String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
         Log.i(TAG_HTTPGET, "HttpGet方法要求乐成,返回数据如下:");
         Log.i(TAG_HTTPGET, result);
     } else {
         Log.i(TAG_HTTPGET, "HttpGet方法要求掉败");
     }
 }

HttpPost方法要求

 public static void requestByHttpPost() throws Exception {
     String path = "https://reg.163.com/logins.jsp";
     // 新建HttpPost工具
     HttpPost httpPost = new HttpPost(path);
     // Post参数
     List<NameValuePair> params = new ArrayList<NameValuePair>();
     params.add(new BasicNameValuePair("id", "helloworld"));
     params.add(new BasicNameValuePair("pwd", "android"));
     // 设置字符集
     HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
     // 设置参数实体
     httpPost.setEntity(entity);
     // 获取HttpClient工具
     HttpClient httpClient = new DefaultHttpClient();
     // 获取HttpResponse实例
     HttpResponse httpResp = httpClient.execute(httpPost);
     // 判断请求是否成功
     if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
         // 获取返回的数据
         String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
         Log.i(TAG_HTTPGET, "HttpPost方法要求乐成,返回数据如下:");
         Log.i(TAG_HTTPGET, result);

     } else {
         Log.i(TAG_HTTPGET, "HttpPost方法要求掉败");
     }
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • Android 异步网络请求框架-Volley 1. 功能介绍 1.1. Volley Volley 是 Goog...
    Brian512阅读 9,340评论 2 40
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,892评论 25 709
  • 有些路不怕一个人走,而是在走的过程中看不到任何的东西,听不到任何的声音,遇不到一个熟知的人!
    七彩酸奶阅读 864评论 0 0
  • 12. 近日所目睹的种种事情,让人觉得世界真是复杂极了。复杂体现在人际关系上,不管你如何用心以诚相待,发现到头来都...
    Nihao123阅读 1,878评论 0 0