Retrofit 2 - Response body null when response status is 422 (unprocessable entity)

前端 未结 3 1410
野性不改
野性不改 2021-02-19 02:37

I\'m using Retrofit to make a POST Request in my web server.

However, I can\'t seem to get the response body when the response status is 422 (unprocessable entit

3条回答
  •  独厮守ぢ
    2021-02-19 03:08

    I got the same error. My API was working using POSTMAN request but not working from Android retrofit call.

    At first I was trying using @Field but it was getting error but later I've tried with @Body and it worked.

    Sample Retrofit interface call

    @POST("api/v1/app/instance")
    Call updateTokenValue(
            @HeaderMap Map headers,
            @Body String body); 
    

    and API calling code is:

     Map headerMap=new HashMap<>();
            headerMap.put("Accept", "application/json");
            headerMap.put("Content-Type", "application/json");
            headerMap.put("X-Authorization","access_token");
    
            Map fields = new HashMap<>();
            fields.put("app_name", "video");
            fields.put("app_version", "2.0.0");
            fields.put("firebase_token", "token");
            fields.put("primary", "1");
    
            ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
            Call call = apiInterface.updateTokenValue(
                    headerMap,new Gson().toJson(fields));
    

提交回复
热议问题