1
votes

This is not a duplicate question. I am using Retrofit 2.0 and json for network tasks. Also I am not using GSON to parse json instead I am using simple JsonObject and JsonArray to get model objects from json string. Firstly guide me which retrofit converter must be used for above scenario.

Secondly, I am not able to get json string as response string.

I tried two approaches - Approach 1 - I used Call< Void >. In this case the response.body() returns null though status code is 200.

Approach 2 - I used Call< ResponseBody >. In this case call.enqueue methods call 'on failure method' instead of 'onSuccess' and also the response body is null. The status code is 200 in this case also.

Please suggest how to get the json string as response from retrofit 2.0.

1

1 Answers

4
votes

you need to use JsonObject instead of Void or ResponseBody. Your code should be

Call<JsonObject> getCall = request.getDataCall();
getCall.enqueue(new Callback<JsonObject>() {
    @Override
    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

    }

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

    }
});

Note : make sure you are using com.google.gson.JsonObject