相关疑难解决方法(0)

OkHttp Post Body as JSON

所以,当我使用Koush的Ion时,我能够通过一个简单的方式在我的帖子中添加一个json体 .setJsonObjectBody(json).asJsonObject()

我正在转向OkHttp,我真的没有看到一个很好的方法来做到这一点.我到处都收到错误400.

有人有主意吗?

我甚至尝试将其手动格式化为json字符串.

String reason = menuItem.getTitle().toString();
JsonObject json = new JsonObject();
json.addProperty("Reason", reason);

String url = mBaseUrl + "/" + id + "/report";

Request request = new Request.Builder()
        .header("X-Client-Type", "Android")
        .url(url)
        .post(RequestBody
                .create(MediaType
                    .parse("application/json"),
                        "{\"Reason\": \"" + reason + "\"}"
                ))
        .build();

client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
    @Override
    public void onFailure(Request request, IOException throwable) {
        throwable.printStackTrace();
    }

    @Override
    public void onResponse(Response response) throws IOException {
        if (!response.isSuccessful()) throw new IOException(
                "Unexpected code " + response);
        runOnUiThread(new Runnable() {
            @Override …
Run Code Online (Sandbox Code Playgroud)

android json http-post android-ion okhttp

60
推荐指数
4
解决办法
9万
查看次数

标签 统计

android ×1

android-ion ×1

http-post ×1

json ×1

okhttp ×1