Java 第三方库总结

1. OKHttp,好用的Http服务

官网地址:http://square.github.io/okhttp/

1) 获取http文件内容

 1 OkHttpClient client = new OkHttpClient();
 2 
 3 String run(String url) throws IOException {
 4   Request request = new Request.Builder()
 5       .url(url)
 6       .build();
 7 
 8   Response response = client.newCall(request).execute();
 9   return response.body().string();
10 }

2)发送POST请求

 1 public static final MediaType JSON
 2     = MediaType.parse("application/json; charset=utf-8");
 3 
 4 OkHttpClient client = new OkHttpClient();
 5 
 6 String post(String url, String json) throws IOException {
 7   RequestBody body = RequestBody.create(JSON, json);
 8   Request request = new Request.Builder()
 9       .url(url)
10       .post(body)
11       .build();
12   Response response = client.newCall(request).execute();
13   return response.body().string();
14 }

 

posted @ 2015-08-11 19:40  周葳  阅读(219)  评论(0编辑  收藏  举报