How to get http response status in synchronous ret

2019-07-14 05:29发布

问题:

I am getting the response from the server all right, using retrofit 2.0.1, Is there any way to get the HTTP response status, without using Asynchronous method?

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.8.4/********/***/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface request = retrofit.create(RequestInterface.class);
        HotelDetail hd;

        Call<HotelDetail> call1 = request.getIndividualHotel("1");

       hd = call1.execute().body();

回答1:

You can store it in Response type and then execute it. Response provides the functionality to retrieve codes via .code() method.

Response response = call1.execute();
    System.out.println(response.code());
    hd = (HotelDetail) response.body();
    System.out.println(hd.toString());