您好我试图将原始json通过对象发送到服务器但没有成功使用Retrofit库。从这个问题中得到了一些建议,但没有成功How to POST raw whole JSON in the body of a Retrofit request? 所以我的代码是:
Prod tag = new Prod();
tag.setName(name);
tag.email(email);
String json = JsonUtilFactory.getJsonUtil().toJson(tag);
TypedJsonString in = new TypedJsonString(json);
service.checkProduct(in, new Callback<Object>()
@PUT("/api/scans")
public void request(@Body TypedJsonString typedInput, Callback<Object> callback);
public class TypedJsonString extends TypedString {
public TypedJsonString(String body) {
super(body);
}
@Override public String mimeType() {
return "application/json";
}
}
因此,每当我向服务器发出请求时,我都会收到这样的错误
D/Retrofit﹕ ---> HTTP PUT https://somehost.com/call
D/Retrofit﹕ Content-Type: application/json
D/Retrofit﹕ Content-Length: 125
D/Retrofit﹕ {"name":"prod_name","email":"my@email"}
D/Retrofit﹕ ---> END HTTP (125-byte body)
I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/14/15, ab0075f, Id3510ff6dc
I/OpenGLRenderer﹕ Initialized EGL, version 1.4
28418-28855/co.otenti D/OpenGLRenderer﹕ Enabling debug mode 0
05-28 21:53:18.768 735-831/? I/ActivityManager﹕ Displayed co.otenti/.activity.MenuActivity: +9s186ms
D/Retrofit﹕ <--- HTTP 404 https://somehost.com/call (933ms)
D/Retrofit﹕ Server: nginx/1.6.2
D/Retrofit﹕ Date: Thu, 28 May 2015 18:53:20 GMT
D/Retrofit﹕ Content-Type: text/html; charset=utf-8
D/Retrofit﹕ Transfer-Encoding: chunked
D/Retrofit﹕ Connection: keep-alive
D/Retrofit﹕ X-Powered-By: Express
D/Retrofit﹕ X-Content-Type-Options: nosniff
D/Retrofit﹕ Set-Cookie: connect.sid=s%3AJagAmIPjbhpZwojw6AatazyL.ZKrtIC31UyHVSyBuN98pvYTaW9I%2BpeNr5XnEDS8yWrw; Path=/; HttpOnly
D/Retrofit﹕ Vary: Accept-Encoding
D/Retrofit﹕ OkHttp-Selected-Protocol: http/1.1
D/Retrofit﹕ OkHttp-Sent-Millis: 1432839199295
D/Retrofit﹕ OkHttp-Received-Millis: 1432839199483
D/Retrofit﹕ Cannot PUT /api/scans
有人可以解释一下,我做错了什么?
此前的代码也是这样的
JSONObject obj = JsonFactory.toJson(prod,Prod.class)
httpPost.setEntity(new StringEntity(obj.toString()));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");