Retrofit: unable to create @Body converter for class

I need to send the following json through modification 2:

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

With title: Authorization: access_token_value

I tried this:

 //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(); 

But every time I got an exception: java.lang.IllegalArgumentException: Unable to create @Body converter for class com.google.gson.JsonObject (parameter #2)

How can I send it? Or any other idea how I can do this. You can even suggest me a parameter with a simple String with json inside. It will suit me

+11
java json retrofit retrofit2
source share
4 answers

Solution: declare a body value in your interface as follows:

@Body RequestBody body and wrap a String JSON object:

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

+18
source share

Body use one request object, declare the request object as follows

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

I assume your IConstructSecureAPI service IConstructSecureAPI endpoint:

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

and you can declare your desire Response .

Mark this answer , its use of HashMap instead of class.

+1
source share

You can use Interceptor to send an authorization header in each request.

 class AuthorizationInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request originalRequest = chain.request(); String authorizationToken = AuthenticationUtils.getToken(); Request authorizedRequest = originalRequest.newBuilder() .header("Authorization", authorizationToken) .build(); return chain.proceed(authorizedRequest); } } 
0
source share

You can specify the converter when creating a modification like this

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

All Articles