2016-03-09 12 views
7

ho bisogno di inviare il prossimo JSON tramite retrofit 2:Retrofit: Impossibile creare convertitore @Body per la classe

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

con l'intestazione: Authorization: access_token_value

ho provato questo:

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

Ma ogni volta ho ricevuto un'eccezione: java.lang.IllegalArgumentException: Unable to create @Body converter for class com.google.gson.JsonObject (parameter #2)

Come posso inviarlo? O qualsiasi altra idea su come posso farlo. Puoi anche offrirmi con parametro come semplice String con json dentro. Sarà adatto per me

+0

Si prega di pubblicare IConstructSecureAPI solo per sapere come si sta costruendo la vostra richiesta. –

risposta

10

Soluzione: valore di dichiarare corpo nella vostra interfaccia con il prossimo:

@Body RequestBody body e avvolgere String oggetto JSON:

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

1

Body uso di un singolo oggetto di richiesta, dichiarare il vostro oggetto di richiesta come segue

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

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

Presumo vostro servizio IConstructSecureAPI endpoint è:

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

ed è possibile dichiarare il tuo desiderio Response.

Controllare questo answer, il suo uso HashMap anziché la classe.

0

È possibile utilizzare un intercettore per inviare intestazione di autorizzazione in ogni richiesta

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); 
    } 
} 
Problemi correlati