Java 第三方庫總結

1. OKHttp,好用的Http服務

官網地址:http://square.github.io/okhttp/git

1) 獲取http文件內容github

 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請求json

 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 }
相關文章
相關標籤/搜索