OkHttp 常用

get:

String url = "";

 

OkHttpClient okHttpClient = new OkHttpClient();

Request request = new Request.Builder() .url(url) .build();
String s = okHttpClient.newCall(request).execute().body().string();

 

post:

# Json格式

RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), String.valueOf(json));

# key-value格式

RequestBody requestBody = new FormBody.Builder().add("key1", value1).add("key2", value2).build();

 

String url = "";
OkHttpClient okHttpClient = new OkHttpClient();

Request request = new Request.Builder().url(url).post(requestBody).build();
return okHttpClient.newCall(request).execute().body().string();

posted @ 2018-12-27 12:03  哦克Oak  阅读(205)  评论(0编辑  收藏  举报