Bartosz Bunzlau
Bartosz Bunzlau

Reputation: 37

How to get response from server as String ,Retrofit 1.9

I would like to handle my response from server , but I don't know how JSON (from server) its looks like. So I tried to display response as String , but I cant do it. Is it possible to display response as String? And then handle the response correctly. thanks

(Retrofit 1.9)

    LoginService loginService = RetrofitClient.createService(LoginService.class);
    loginService.searchOffer(getToken(), offerSearch, new Callback<String>() {
        @Override
        public void success(String offerSearchRequestResource, Response response) {
            String responseFromSercer = response.getBody();

        }

        @Override
        public void failure(RetrofitError error) {

        }
    });

Upvotes: 0

Views: 684

Answers (2)

Reza Abedi
Reza Abedi

Reputation: 448

change your response model to

JSONObject (from Gson)

then in your

public void success(...){response.body.toString();}

like this:

 Call<JsonObject> call = YOUR_API_INTERFACE().YOUR_METHOD(YOUR_PARAMS);
    call.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, @NonNull Response<JsonObject> response) {
            if(response.isSuccessful()) {
                String responseFromSercer = response.body.toString();
            }
        }

        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            .....
        }
    });

Upvotes: 1

Makamu Evans
Makamu Evans

Reputation: 541

If you are sure the request runs successfully and there is a response back...use

System.out.println(response.getBody());

i'd also suggest you add Logging Interceptors here so that you can get a detailed view of all your API calls

Upvotes: 0

Related Questions