okhttp使用总结

本文详细介绍了如何在 Android 中使用 OkHttp 进行网络请求,包括同步和异步 GET、POST 请求,设置和获取 Header,文件上传,缓存处理,超时设置,以及使用 Interceptor 进行拦截器操作。内容涵盖了 OkHttp 的基本用法和高级特性,适合 Android 开发者参考。
摘要由CSDN通过智能技术生成

okhttp 使用总结

okhttp 的使用越来越火,有必要对其进行研究。以下博客中的例子为了简单,在Eclipse中通过JAVA工程进行验证。

导入OkHttp

AndroidStudio

添加如下代码即可

 compile 'com.squareup.okhttp3:okhttp:3.3.1'
 compile 'com.squareup.okio:okio:1.8.0'

其中okiookhttp中关于流的操作。必须导入。

Eclipse

导入okhttpokio的jar包即可。

下载地址okhttp3.3.1与okio-1.8.0

同步的GET请求

主要分为以下几步:

  • 创建OkHttpClient对象
  • 根据需求创建Request对象,Request中主要包含了请求的url,参数等信息。
  • 通过request对象创建Call对象
  • 使用call对象进行网络请求。返回封装好的Response
  • 解析Response对象,获取响应信息
/**
*  同步 Get 请求
*/
public static void syncGet() throws Exception {

        // 1. 创建`OkHttpClient`对象
        final OkHttpClient client = new OkHttpClient();

        // 2.根据需求创建`Request`对象.在此只是添加了最基本的url
        Request request = new Request.Builder().url(BAIDU_MP3_PATH).build();

        // 3.通过`request`对象创建`Call`对象
        Call call = client.newCall(request);

        // 4.使用`call`对象进行网络请求。返回封装好的`Response`
        Response response = call.execute();

        // 5.解析`Response`对象,获取响应信息
        if (!response.isSuccessful())
            // code >= 200 && code < 300;
            System.out.println(response);

        Headers responseHeaders = response.headers();

        for (int i = 0; i < responseHeaders.size(); i++) {
            System.out.println(responseHeaders.name(i) + ":"
                    + responseHeaders.value(i));
        }

        // 打印响应的内容
        System.out.println(response.body().string());

    }

其中使用的url为百度MP3的接口:

http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.billboard.billList&format=json&type=1&offset=0&size=1

看一下打印结果:


//---- response 的toString() 默认打印的信息  响应行
Response{protocol=http/1.1, code=200, message=OK, url=http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.billboard.billList&format=json&type=1&offset=0&size=1}


//----- 响应的头
Date:Wed, 29 Jun 2016 01:51:39 GMT
Content-Type:application/json
Transfer-Encoding:chunked
Connection:keep-alive
X-LIGHTTPD-LOGID:427084050
RT:427084050_d41d8cd98f00b204e9800998ecf8427e_d41d8cd98f00b204e9800998ecf8427e
Server:Apache1.0
SC:MISS.0.0
tracecode:30991485713674384576062909
Set-Cookie:BAIDUID=B343E34EB3618E7E2E867A6E1D4F04EB:FG=1; expires=Thu, 29-Jun-17 01:51:39 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1
P3P:CP=" OTI DSP COR IVA OUR IND COM "


// ---- 响应的内容  body
{
  "song_list":[{
  "artist_id":"247010469","language":"\u56fd\u8bed","pic_big":"http:\/\/musicdata.baidu.com\/data2\/pic\/0c87cb20fd6b543cd1c90d1d82f1f6c5\/266782982\/266782982.jpg","pic_small":"http:\/\/musicdata.baidu.com\/data2\/pic\/169cbb0d82522568dea198363f7028ec\/266782985\/266782985.jpg","country":"\u5185\u5730","area":"0","publishtime":"2016-06-20","album_no":"1","lrclink":"http:\/\/musicdata.baidu.com\/data2\/lrc\/1f75306d7f1e25f723d3d175f5ef3d46\/266791084\/266791084.lrc","copy_type":"1","hot":"276927","all_artist_ting_uid":"232954914","resource_type":"0","is_new":"1","rank_change":"0","rank":"1","all_artist_id":"247010469","style":"\u6d41\u884c","del_status":"0","relate_status":"0","toneid":"0","all_rate":"64,128,256","sound_effect":"0","file_duration":0,"has_mv_mobile":1,"versions":"","bitrate_fee":"{\"0\":\"0|0\",\"1\":\"-1|-1\"}","song_id":"266784254","title":"\u7ea2","ting_uid":"232954914","author":"\u51af\u5efa\u5b87","album_id":"266784256","album_title":"\u7ea2","is_first_publish":0,"havehigh":0,"charge":0,"has_mv":1,"learn":0,"song_source":"web","piao_id":"0","korean_bb_song":"1","resource_type_ext":"1","mv_provider":"0100000000","artist_name":"\u51af\u5efa\u5b87"}],"billboard":{
  "billboard_type":"1","billboard_no":"1871","update_date":"2016-06-28","billboard_songnum":"190","havemore":1,"name":"\u65b0\u6b4c\u699c","comment
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值