neustart47 neustart47 - 1 year ago 664
Java Question

Retrofit: Unable to create @Body converter for class

I need to send next json via retrofit 2:

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

With Header:
Authorization: access_token_value

I tried this:

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

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


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

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

Retrofit restAdapter = new Retrofit.Builder()
IConstructSecureAPI service = restAdapter.create(IConstructSecureAPI.class);
Call<JsonElement> result = service.addInspection(accessToken, obj);
JsonElement element = result.execute().body();

But everytime i recieved exception:
java.lang.IllegalArgumentException: Unable to create @Body converter for class (parameter #2)

How can I send it ? Or any another idea how I can do it. You can even offer me with parameter as simple
with json inside. It will suit for me

Answer Source

Solution: declare body value in your interface with next:

@Body RequestBody body and wrap String JSON object:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download