abouttr3

Snippet OkHttp

Nov 3rd, 2016
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1.     public String requestOkHttp(String url, String method, String json) throws IOException {
  2.         RequestBody requestBody;
  3.         requestBody = METHOD_POST.equals(method) || METHOD_PUT.equals(method) ?
  4.                 RequestBody.create(JSON, json == null ? "" : json) : null;
  5.  
  6.         okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder()
  7.                 .url(url);
  8.  
  9.         requestBuilder.method(method, requestBody);
  10.  
  11.         OkHttpClient client = new OkHttpClient();
  12.  
  13.         Response response = client.newCall(requestBuilder.build()).execute();
  14.         return response.body().string();
  15.     }
Advertisement
Add Comment
Please, Sign In to add comment