【发布时间】:2016-03-14 19:00:10
【问题描述】:
所以,当我使用 Koush 的 Ion 时,我可以通过简单的 .setJsonObjectBody(json).asJsonObject() 将 json 正文添加到我的帖子中
我正在迁移到 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
public void run() {
Toast.makeText(context, "Report Received", Toast.LENGTH_SHORT).show();
}
});
}
});
/*Ion.with(getContext(), url)
.setHeader("X-Client-Type", "Android")
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
Toast.makeText(context, "Report Received", Toast.LENGTH_SHORT).show();
}
});*/
【问题讨论】:
-
您的网址是否在开始时包含“http://”?
-
https://,实际上,但是是的
-
您是否信任过您应用的证书?
-
好吧,看到我得到 {"Reason":"Inappropriate"} Response{protocol=http/1.1, code=200, message=OK, url=api/id/report} {"Reason": “版权”} 响应{protocol=http/1.1, code=400, message=Bad Request, url=api/id/report} 23 分钟
-
它取出了我的 https:// 作为 url=
标签: android json http-post okhttp android-ion