OkHttp的基本使用——替代Apache HttpClient

http是现在主流应用使用的网络请求方式, 用来交换数据和内容, 有效的使用HTTP可以使你的APP 变的更快和减少流量的使用

OkHttp 是一个很棒HTTP客户端:

  • 支持SPDY, 可以合并多个到同一个主机的请求
  •  使用连接池技术减少请求的延迟(如果SPDY是可用的话)
  •  使用GZIP压缩减少传输的数据量
  •  缓存响应避免重复的网络请求

OkHttp可以替换Apache的HttpClient

OkHttp支持2.3和以上版本,对于java,需要jdk1.7 ,OkHttp需要依赖Okio

下面上demo

1:使用get方式请求,获取响应

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import okhttp3.OkHttpClient;  
  3. import okhttp3.Request;  
  4. import okhttp3.Response;  
  5.   
  6. public class GetExample {  
  7.   OkHttpClient client = new OkHttpClient();  
  8.   
  9.   String run(String url) throws IOException {  
  10.     Request request = new Request.Builder()  
  11.         .url(url)  
  12.         .build();  
  13.   
  14.     try (Response response = client.newCall(request).execute()) {  
  15.       return response.body().string();  
  16.     }  
  17.   }  
  18.   
  19.   public static void main(String[] args) throws IOException {  
  20.     GetExample example = new GetExample();  
  21.     String response = example.run("https://raw.github.com/square/okhttp/master/README.md");  
  22.     System.out.println(response);  
  23.   }  
  24. }  

2:使用post向服务器发送请求

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import okhttp3.MediaType;  
  3. import okhttp3.OkHttpClient;  
  4. import okhttp3.Request;  
  5. import okhttp3.RequestBody;  
  6. import okhttp3.Response;  
  7.   
  8. public class PostExample {  
  9.   public static final MediaType JSON  
  10.       = MediaType.parse("application/json; charset=utf-8");  
  11.   
  12.   OkHttpClient client = new OkHttpClient();  
  13.   
  14.   String post(String url, String json) throws IOException {  
  15.     RequestBody body = RequestBody.create(JSON, json);  
  16.     Request request = new Request.Builder()  
  17.         .url(url)  
  18.         .post(body)  
  19.         .build();  
  20.     try (Response response = client.newCall(request).execute()) {  
  21.       return response.body().string();  
  22.     }  
  23.   }  
  24.   
  25.   String bowlingJson(String player1, String player2) {  
  26.     return "{'winCondition':'HIGH_SCORE',"  
  27.         + "'name':'Bowling',"  
  28.         + "'round':4,"  
  29.         + "'lastSaved':1367702411696,"  
  30.         + "'dateStarted':1367702378785,"  
  31.         + "'players':["  
  32.         + "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39},"  
  33.         + "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}"  
  34.         + "]}";  
  35.   }  
  36.   
  37.   public static void main(String[] args) throws IOException {  
  38.     PostExample example = new PostExample();  
  39.     String json = example.bowlingJson("Jesse""Jake");  
  40.     String response = example.post("http://www.roundsapp.com/post", json);  
  41.     System.out.println(response);  
  42.   }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值