2

I got the following response from my server: status code 201 Created. There is no actual response (returned object, etc.), so there is not need to create a POJO class.

So, I don't know how I should handle this status code without creating a POJO class. Is there any option to make write the code without using a POJO class?

TommySM
  • 3,793
  • 3
  • 24
  • 36
Shree
  • 43
  • 4

2 Answers2

4

Retrofit API has Response class that can encapsulate your response.

As long as you don't want to bother with the response data you can implement your service as:

interface CustomService {
    @GET("whatever")
    Call<Response<Void>> getAll();

    // Or using RxJava:
    @GET("whatever")
    Single<Response<Void>> getRxAll();
}

Then implement your callback:

private Callback<Response<Void>> responseHandler = new Callback<Response<Void>>() {
  @Override
  public void onResponse(Call<Response<Void>> call, Response<Response<Void>> response) {
    final int code = response.code();
    // TODO: Do whatever you want with the response code.
  }

  @Override
  public void onFailure(Call<Response<Void>> call, Throwable t) {
      // TODO: Handle failure.
  }
}

Or reactive consumer:

private Consumer<Response<Void>> responseRxHandler = new Consumer<Response<Void>>() {
  @Override
  public void accept(Response<Void> response) throws Exception {
      final int responseCode = response.code();
      // TODO: Do whatever you want with the response code.
  }
};

Debugging result:enter image description here

Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
3

You can try the following code.

Can get the response without a POJO class by getting using ResponseBody format and then you can parse it normally like ordinary JSON parsing.

Api Call:

Call<ResponseBody> call = service.callLogin(AppConstants.mApiKey, model_obj);

    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if(response.code() == 201)
    {
     JSONObject jobjresponse = null;
    try {
            jobjresponse = new JSONObject(mResponse.body().string());
            String status = jobjresponse.getString("status");
            JSONObject  result = jobjresponse.getJSONObject("results");
                    String  msg = result.getString(“msg”);  
} catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }       

        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });

Retrofit Interface class:

public interface RetrofitInterface {
@Headers({"Content-Type: application/json", "Cache-Control: max-age=640000"})
@POST("v1/auth/")
public Call<ResponseBody> callLogin(@Query("key") String key, @Body LoginModel body);

public static final Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(“base url”)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
}

Sample Response:

{ "status":"true", "result":{"msg”:”created successfully”} }
TommySM
  • 3,793
  • 3
  • 24
  • 36
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19