从头学android_HttpClient

HttpClient(已过时)

这是在android 6.0 之后被谷歌隐藏的网络连接的api,被谷歌建议使用的是HttpURLConnection。

关于HttpURLConnection的使用,可以参考我的另外一篇博客 从头学android_GET和POST请求

如果真的必须要使用这个api,谷歌也提供了在android studio中的解决方法。
附上谷歌官方原文:

/*--------------------------------------------------------*/

//官方原文

Apache HTTP Client Removal

Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:

 

android {

    useLibrary 'org.apache.http.legacy'

}

/*--------------------------------------------------------*/

 

原文地址:http://developer.android.com/intl/zh-cn/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client


但是一些老的程序是用这个API写的,所以对这个过时方法仍然有必要学习。


GET:

                String path = "http://172.20.12.131:8080/web001/LoginServlet?username=" + URLEncoder.encode(username) + "&password=" + password;
                //创建client对象
                HttpClient client = new DefaultHttpClient();
                //创建get请求对象
                HttpGet get = new HttpGet(path);
                try {
                    //使用client发送get请求,得到相应对象
                    HttpResponse response = client.execute(get);
                    //获取状态行,状态行包含着状态信息
                    StatusLine line = response.getStatusLine();
                    if (line.getStatusCode() ==200){//响应码是200,交互成功
                        //获取实体
                        HttpEntity entity = response.getEntity();
                        //获取内容
                        InputStream is = entity.getContent();
                        String result = getStringFromInputStream(is);

                        Message msg = new Message();
                        msg.obj = result;
                        handler.sendMessage(msg);
                    }
                }

POST:

与get不同的是,需要在httpPost中附加参数信息

                String path = "http://172.20.12.131:8080/web001/LoginServlet";
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(path);

                List<NameValuePair> parameters = new ArrayList<>();
                NameValuePair nvp1 = new BasicNameValuePair("username",username);
                NameValuePair nvp2 = new BasicNameValuePair("password",password);
                parameters.add(nvp1);
                parameters.add(nvp2);
                HttpEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");
                post.setEntity(entity);

总结

使用HttpClient显然要比HttpUrlConnection要繁琐,虽然结构更 清晰了,但几乎没有太大的封装进步,如果有更简捷的使用要求,完全可以使用一些第三方的框架实现,所以开发中应尽量遵照谷歌的规范建议,避免使用HttpClient。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值