HttpClient和HttpURLConnection

对于HttpClient和HttpURLConnection我们该怎么选择呢?

  1)在Android2.2版本之前,由于HttpClient有较少的bug,因此我们选择它来使用。
  2)然而在Android 2.3版本及以后,HttpURLConnection则是最佳的选择。HttpUrlConnection对大部分工作进行了包装,屏蔽了不需要的细节,体积较小,因而非常适用于Android项目。并且HttpUrlConnection直接在系统层面做了缓存策略处理,可以加快重复请求的速度。由于其压缩(GZip)和缓存机制可以有效地减少网络访问的流量,在提升速度和省电方面也起到了较大的作用。对于新的应用程序应该更加偏向于使用HttpURLConnection,因为在后续Google官方会将更多的时间放在优化HttpURLConnection上面。

Http请求方式

GET:请求指定Url的数据,请求体为空(例如打开网页)。
POST:请求指定Url的数据,同时传递参数(在请求体中)。

Get和Post

GET:在请求的URL地址后以?的形式带上交给服务器的数据,多个数据之间以&进行分隔, 但数据容量通常不能超过2K,比如:
http://xxx?username=…&pawd=…
POST:这个则可以在请求的实体内容中向服务器发送数据,传输没有数量限制。
  这两个都是发送数据的,只是发送机制不一样。Get安全性非常低,Post安全性较高,Get执行效率比Post高。一般查询的时候我们用GET,数据增删改的时候用POST。

Http状态码

200:OK,客户端请求成功。
403:Forbidden,服务器收到请求,但是拒绝提供服务。
404:Not Found,请求资源不存在。
......

示例:使用Get和Post请求

  1)添加访问网络的权限

<uses-permission android:name="android.permission.INTERNET"/>

  2)Callback监听器
HttpCallbackListener.java

/**
 * HttpURLConnection网络请求返回监听器
 */
public interface HttpCallbackListener {
    // 网络请求成功
    void onFinish(byte[] response);

    // 网络请求失败
    void onError(Exception e);
}

  3)网络请求工具类
HttpUtil.java

/**
 * HttpURLConnection网络请求工具类
 */
public class HttpUtil {
    /**
     * Get请求
     */
    public static void sendGetRequest(
            final String urlString, final HttpCallbackListener listener) {
        // 因为网络请求是耗时操作,所以需要另外开启一个线程来执行该任务。
        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url;
                HttpURLConnection httpURLConnection = null;
                try {
                    // 根据URL地址创建URL对象
                    url = new URL(urlString);
                    // 获取HttpURLConnection对象
                    httpURLConnection = (HttpURLConnection) url.openConnection();
                    // 设置请求方式,默认为GET
                    httpURLConnection.setRequestMethod("GET");
                    // 设置连接超时
                    httpURLConnection.setConnectTimeout(5000);
                    // 设置读取超时
                    httpURLConnection.setReadTimeout(8000);
                    /*// 如果需要设置apikey,如下设置:
                    httpURLConnection.setRequestProperty(
                    "apikey", "1b18****13f3****729210d6****8e29");*/

                    // 响应码为200表示成功,否则失败。
                    if (httpURLConnection.getResponseCode() != 200) {
                        Logger.e("请求失败");
                    }
                    // 获取网络的输入流
                    InputStream is = httpURLConnection.getInputStream();
                    // 读取输入流中的数据
                    BufferedInputStream bis = new BufferedInputStream(is);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] bytes = new byte[1024];
                    int len = -1;
                    while ((len = bis.read(bytes)) != -1) {
                        baos.write(bytes, 0, len);
                    }
                    bis.close();
                    is.close();
                    // 响应的数据
                    byte[] response = baos.toByteArray();

                    if (listener != null) {
                        // 回调onFinish()方法
                        listener.onFinish(response);
                    }
                } catch (MalformedURLException e) {

                    if (listener != null) {
                        // 回调onError()方法
                        listener.onError(e);
                    }
                } catch (IOException e) {

                    if (listener != null) {
                        // 回调onError()方法
                        listener.onError(e);
                    }
                } finally {
                    if (httpURLConnection != null) {
                        // 释放资源
                        httpURLConnection.disconnect();
                    }
                }
            }
        }).start();
    }

    /**
     * Post请求
     */
    public static void sendPostRequest(
            final String urlString, final HttpCallbackListener listener) {

        // 因为网络请求是耗时操作,所以需要另外开启一个线程来执行该任务。
        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url;
                HttpURLConnection httpURLConnection = null;
                try {
                    url = new URL(urlString);
                    httpURLConnection = (HttpURLConnection) url.openConnection();

                    httpURLConnection.setRequestMethod("POST");

                    httpURLConnection.setConnectTimeout(5000);
                    httpURLConnection.setReadTimeout(8000);

                    // 设置运行输入
                    httpURLConnection.setDoInput(true);
                    // 设置运行输出
                    httpURLConnection.setDoOutput(true);

                    // 请求的数据
                    String data = "num=" + URLEncoder.encode("10", "UTF-8") +
                            "&page=" + URLEncoder.encode("1", "UTF-8");

                    // 将请求的数据写入输出流中
                    OutputStream os = httpURLConnection.getOutputStream();
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    bos.write(data.getBytes());
                    bos.flush();
                    bos.close();
                    os.close();

                    if (httpURLConnection.getResponseCode() == 200) {

                        InputStream is = httpURLConnection.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);

                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] bytes = new byte[1024];
                        int len = -1;
                        while ((len = bis.read(bytes)) != -1) {
                            baos.write(bytes, 0, len);
                        }
                        is.close();
                        bis.close();
                        // 响应的数据
                        byte[] response = baos.toByteArray();

                        if (listener != null) {
                            // 回调onFinish()方法
                            listener.onFinish(response);
                        }
                    } else {
                        Logger.e("请求失败");
                    }
                } catch (MalformedURLException e) {
                    if (listener != null) {
                        // 回调onError()方法
                        listener.onError(e);
                    }
                } catch (IOException e) {
                    if (listener != null) {
                        // 回调onError()方法
                        listener.onError(e);
                    }
                } finally {
                    if (httpURLConnection != null) {
                        // 最后记得关闭连接
                        httpURLConnection.disconnect();
                    }
                }
            }
        }).start();
    }
}

  4)Activity的布局
activity_http_url_connection.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <ImageView
        android:id="@+id/show_image"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_gravity="center"
        android:scaleType="centerCrop"/>

    <Button
        android:id="@+id/get_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取网络图片"/>
</LinearLayout>

  4)在Activity中使用
HttpURLConnectionActivity.java

/**
 * HttpURLConnection
 */
public class HttpURLConnectionActivity extends BaseActivity {

    @BindView(R.id.show_image)
    ImageView showImage;
    @BindView(R.id.get_image)
    Button getImage;

    private Bitmap bitmap;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x001:
                    showImage.setImageBitmap(bitmap);
                    break;
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_http_url_connection;
    }

    @OnClick(R.id.get_image)
    public void onClick() {
        HttpUtil.sendGetRequest(
                "https://pic3.zhimg.com/2585173a01d52b49d9714e2c50801d15.jpg",
                new HttpCallbackListener() {
                    @Override
                    public void onFinish(byte[] response) {
                        bitmap = BitmapFactory.decodeByteArray(response, 0, response.length);
                        handler.sendEmptyMessage(0x001);
                    }

                    @Override
                    public void onError(Exception e) {
                        // 在这里对异常情况进行处理
                        Logger.e(e.getMessage());
                    }
                });
    }
}


作者:李俊的博客
链接:http://www.jianshu.com/p/a79f1f49371b
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值