2016-03-09 25 views
7

tôi cần phải gửi json tiếp theo thông qua trang bị thêm 2:Retrofit: Không thể tạo chuyển đổi @Body cho lớp

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

Với tiêu đề: Authorization: access_token_value

Tôi cố gắng này:

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

Nhưng mọi lúc tôi nhận được ngoại lệ: java.lang.IllegalArgumentException: Unable to create @Body converter for class com.google.gson.JsonObject (parameter #2)

Tôi làm cách nào để gửi? Hoặc bất kỳ ý tưởng nào khác về cách tôi có thể làm điều đó. Bạn thậm chí có thể cung cấp cho tôi với tham số đơn giản như String với json bên trong. Nó sẽ phù hợp đối với tôi

+0

Vui lòng đăng IConstructSecureAPI chỉ để biết bạn đang xây dựng yêu cầu của mình như thế nào. –

Trả lời

10

Giải pháp: khai báo giá trị cơ thể trong giao diện của bạn với tiếp theo:

@Body RequestBody body và quấn Chuỗi đối tượng JSON:

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

1

Body sử dụng một đối tượng yêu cầu duy nhất, tuyên bố đối tượng yêu cầu của bạn như sau

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

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

tôi giả sử điểm cuối dịch vụ IConstructSecureAPI của bạn là:

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

và bạn có thể khai báo mong muốn của bạn Response.

Kiểm tra điều này answer, việc sử dụng nó HashMap thay vì lớp học.

0

Bạn có thể sử dụng một Interceptor hãy gửi Authorization header trong mỗi yêu cầu

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); 
    } 
} 
Các vấn đề liên quan