okhttp 使用总结
okhttp
的使用越来越火,有必要对其进行研究。以下博客中的例子为了简单,在Eclipse
中通过JAVA
工程进行验证。
导入OkHttp
AndroidStudio
添加如下代码即可
compile 'com.squareup.okhttp3:okhttp:3.3.1'
compile 'com.squareup.okio:okio:1.8.0'
其中okio
是okhttp
中关于流的操作。必须导入。
Eclipse
导入okhttp
与okio
的jar包即可。
同步的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