改造:无法为类创建@Body转换器

时间:2016-03-09 00:46:34

标签: java json retrofit retrofit2

我需要通过改装2发送下一个json:

{
    "Inspection": {
        "UUID": "name",
        "ModifiedTime": "2016-03-09T01:13",
        "CreatedTime": "2016-03-09T01:13",
        "ReviewedWith": "name2",
        "Type": 1,
        "Project": {
            "Id": 41
        },
        "ActionTypes": [1]
    }   
}

使用标题:Authorization: access_token_value

我试过了:

//header parameter
String accessToken = Requests.getAccessToken();

JsonObject obj = new JsonObject();
JsonObject inspection = new JsonObject();

inspection.addProperty("UUID","name");
inspection.addProperty("ModifiedTime","2016-03-09T01:13");
inspection.addProperty("CreatedTime","2016-03-09T01:13");
inspection.addProperty("ReviewedWith","name2");
inspection.addProperty("Type","1");

JsonObject project = new JsonObject();
project.addProperty("Id", 41);

inspection.add("Project", project);
obj.add("Inspection", inspection);

Retrofit restAdapter = new Retrofit.Builder()
        .baseUrl(Constants.ROOT_API_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .addConverterFactory(ScalarsConverterFactory.create())
        .build();
IConstructSecureAPI service = restAdapter.create(IConstructSecureAPI.class);
Call<JsonElement> result = service.addInspection(accessToken, obj);
JsonElement element = result.execute().body();

但每次我收到异常:java.lang.IllegalArgumentException: Unable to create @Body converter for class com.google.gson.JsonObject (parameter #2)

我该怎么发送?或者任何其他想法我怎么做。您甚至可以在内部使用json为我提供简单String参数。它适合我

6 个答案:

答案 0 :(得分:16)

<强>解决方案: 在接口中使用next声明body值:

@Body RequestBody body 并包装String JSON对象:

RequestBody body = RequestBody.create(MediaType.parse("application/json"), obj.toString());

答案 1 :(得分:6)

您可能会为多个可变项/字段/标签保留相同的@SerializedName(“”)

答案 2 :(得分:1)

Body 使用单个请求对象,将请求对象声明如下

class Inspection {
    String UUID;
    //..... add your fields 
    Project project;      
}

class Product
{
   int Id;
   //....... add your fields 
}

我假设您的服务IConstructSecureAPI端点是:

@GET(...)    // change based on your api GET/POST
Call<Response> addInspection(
    @Header("Authorization") String accesstoken, 
    @Body Inspection request
);

您可以宣布您的愿望Response

选中此answer,使用HashMap代替课程。

答案 3 :(得分:1)

像这样创建改造时,您可以指定转换器

Retrofit retrofit = new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(baseurl)
        .client(okHttpClient)
        .build();

答案 4 :(得分:0)

您可以使用拦截器在每个请求中发送授权标头

No.

Selectors are meant to address parts of a document, not to transform it.
Although some elementary things are possible,
like striping namespaces and running a regex over a string result,
transformations are out of the scope of this project for now
(and practically they will remain so in the near future).

You should look into xslt or some similar technology.

答案 5 :(得分:0)

如果这是由于setunion造成的,请确保它没有重复。

例如在以下情况下将引发此错误:(注意:@SerializedName传递了两次)

bookingId

但是,这是正确的:

@SerializedName(value="bookingId", alternate={"id", "bookingId"})