这个问题以前可能被问过,但没有明确的答案。如何在Retrofit请求的主体中发布原始的整个JSON ?
在这里看到类似的问题。或者这个答案是正确的,它必须是表单url编码并作为一个字段传递?我真的希望不是,因为我要连接的服务只是希望在文章正文中有原始JSON。它们不是用来为JSON数据寻找特定字段的。
I just want to clarify this with the restperts once and for all. One person answered not to use Retrofit. The other was not certain of the syntax. Another thinks yes it can be done but only if its form url-encoded and placed in a field (that's not acceptable in my case). No, I can't re-code all the services for my Android client. And yes, it's very common in major projects to post raw JSON instead of passing over JSON content as field property values. Let's get it right and move on. Can someone point to the documentation or example that shows how this is done? Or provide a valid reason why it can/should not be done.
更新:有一件事我可以百分之百确定。你可以在谷歌的Volley中做到这一点。这是与生俱来的。我们可以在Retrofit中这样做吗?
使用JsonObject是它的方式:
Create your interface like this:
public interface laInterfaz{
@POST("/bleh/blah/org")
void registerPayer(@Body JsonObject bean, Callback<JsonObject> callback);
}
Make the JsonObject acording to the jsons structure.
JsonObject obj = new JsonObject();
JsonObject payerReg = new JsonObject();
payerReg.addProperty("crc","aas22");
payerReg.addProperty("payerDevManufacturer","Samsung");
obj.add("payerReg",payerReg);
/*json/*
{"payerReg":{"crc":"aas22","payerDevManufacturer":"Samsung"}}
/*json*/
Call the service:
service.registerPayer(obj, callBackRegistraPagador);
Callback<JsonObject> callBackRegistraPagador = new Callback<JsonObject>(){
public void success(JsonObject object, Response response){
System.out.println(object.toString());
}
public void failure(RetrofitError retrofitError){
System.out.println(retrofitError.toString());
}
};
这就是它!在我个人看来,这比制作pojo和处理班级混乱要好得多。这样干净多了。
根据上面的答案,我有一个解决方案,不必为每个请求都创建pojo。
例如,我想发布这个JSON。
{
"data" : {
"mobile" : "qwer",
"password" : "qwer"
},
"commom" : {}
}
然后,我创建一个像这样的公共类:
import java.util.Map;
import java.util.HashMap;
public class WRequest {
Map<String, Object> data;
Map<String, Object> common;
public WRequest() {
data = new HashMap<>();
common = new HashMap<>();
}
}
最后,当我需要一个json
WRequest request = new WRequest();
request.data.put("type", type);
request.data.put("page", page);
然后可以将标记了@Body的请求传递给Retrofit。
@Body注释定义了一个请求主体。
interface Foo {
@POST("/jayson")
FooResponse postJson(@Body FooRequest body);
}
由于Retrofit默认使用Gson,因此FooRequest实例将被序列化为JSON作为请求的唯一主体。
public class FooRequest {
final String foo;
final String bar;
FooRequest(String foo, String bar) {
this.foo = foo;
this.bar = bar;
}
}
调用:
FooResponse = foo.postJson(new FooRequest("kit", "kat"));
将产生以下主体:
{"foo":"kit","bar":"kat"}
Gson文档中有更多关于对象序列化如何工作的内容。
现在,如果你真的非常想自己发送“原始”JSON作为正文(但请使用Gson !),你仍然可以使用TypedInput:
interface Foo {
@POST("/jayson")
FooResponse postRawJson(@Body TypedInput body);
}
TypedInput被定义为“具有关联mime类型的二进制数据”。有两种方法可以使用上述声明轻松发送原始数据:
Use TypedByteArray to send raw bytes and the JSON mime type:
String json = "{\"foo\":\"kit\",\"bar\":\"kat\"}";
TypedInput in = new TypedByteArray("application/json", json.getBytes("UTF-8"));
FooResponse response = foo.postRawJson(in);
Subclass TypedString to create a TypedJsonString class:
public class TypedJsonString extends TypedString {
public TypedJsonString(String body) {
super(body);
}
@Override public String mimeType() {
return "application/json";
}
}
And then use an instance of that class similar to #1.