OkHttp 是否有更简单的类似 Unirest 的字段方法来创建 RequestBody?
Does OkHttp have something easier similar to Unirest's field method for creating a RequestBody?
而不是 Unirest
,我使用的是 okhttp,因为有些响应我只需要 header,所以我不需要使用它的 ResponseBody.string()
方法下载它。
然而,我很难用 RequestBody
构建我对 POSTs
的请求。在Unirest, you only need to use the
字段method to add to the its
RequestBody`:
Unirest.post(baseUrl + "/api/user")
.header("Authorization", token)
.field("id", id)
.field("property", property)
.asJson();
但在 OkHttp
中,如果我想添加一个 RequestBody
,我必须这样做(在大多数情况下,我并没有 json
):
OkHttpClient httpClient = new OkHttpClient();
Map<String, Object> payload = new HashMap<>();
payload.put("user_id", userId);
payload.put("client_id", clientId);
payload.put("type", status.getStatus());
payload.put("description", "");
payload.put("duration", 0);
String requestBody = new ObjectMapper().writeValueAsString(payload);
Call call = httpClient.newCall(
new Request.Builder()
.url(url)
.header("Authorization", tempToken)
.post(RequestBody.create(MediaType.get("application/json"), requestBody))
.build()
);
我正在使用 Map
创建 json
字符串,因为这是我创建 json
.
的最简单方法
OkHttp
是否有类似于 Unirest
的 field
创建 RequestBody 的更简单的方法?
我发现 okhttp
有 FormBody
其中有一个 Builder
:
Call call = httpClient.newCall(
new Request.Builder()
.url(url)
.header("Authorization", tempToken)
.post(new FormBody.Builder()
// TODO user getId()
.add("id","")
.add("custom_fields", field)
.build())
.build()
);
OkHttp 没有 asJson。您可以添加自定义拦截器或实现单独的 JSONObject.
使用JSON对象构建JSON:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", "yourEmail@com");
jsonObject.put("password", "yourPassword");
jsonObject.put("anyKey", "anyValue");
} catch (JSONException e) {
e.printStackTrace();
}
然后像这样调用 OkHttp:
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
okhttp作者使用字符串:https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/guide/PostExample.java
而不是 Unirest
,我使用的是 okhttp,因为有些响应我只需要 header,所以我不需要使用它的 ResponseBody.string()
方法下载它。
然而,我很难用 RequestBody
构建我对 POSTs
的请求。在Unirest, you only need to use the
字段method to add to the its
RequestBody`:
Unirest.post(baseUrl + "/api/user")
.header("Authorization", token)
.field("id", id)
.field("property", property)
.asJson();
但在 OkHttp
中,如果我想添加一个 RequestBody
,我必须这样做(在大多数情况下,我并没有 json
):
OkHttpClient httpClient = new OkHttpClient();
Map<String, Object> payload = new HashMap<>();
payload.put("user_id", userId);
payload.put("client_id", clientId);
payload.put("type", status.getStatus());
payload.put("description", "");
payload.put("duration", 0);
String requestBody = new ObjectMapper().writeValueAsString(payload);
Call call = httpClient.newCall(
new Request.Builder()
.url(url)
.header("Authorization", tempToken)
.post(RequestBody.create(MediaType.get("application/json"), requestBody))
.build()
);
我正在使用 Map
创建 json
字符串,因为这是我创建 json
.
OkHttp
是否有类似于 Unirest
的 field
创建 RequestBody 的更简单的方法?
我发现 okhttp
有 FormBody
其中有一个 Builder
:
Call call = httpClient.newCall(
new Request.Builder()
.url(url)
.header("Authorization", tempToken)
.post(new FormBody.Builder()
// TODO user getId()
.add("id","")
.add("custom_fields", field)
.build())
.build()
);
OkHttp 没有 asJson。您可以添加自定义拦截器或实现单独的 JSONObject.
使用JSON对象构建JSON:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", "yourEmail@com");
jsonObject.put("password", "yourPassword");
jsonObject.put("anyKey", "anyValue");
} catch (JSONException e) {
e.printStackTrace();
}
然后像这样调用 OkHttp:
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
okhttp作者使用字符串:https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/guide/PostExample.java