Retrofit. Android. Can I download json object to String without parsing?

android, json, retrofit

Solution

This happens because the response is a JSON Object and not a String. You could do the following: First change the getter method:

@GET("/api.php?action=bundle")
public void getWholeScheduleString(Callback<JsonObject> response);

Then in the `success(JsonObject json, Response res)` method of your Callback you just map it to a String:

@Override
public void success(JsonObject response, Response arg1) {
    String myResponse = response.getAsString();
}

EDIT: By the way, you can parameterize the query part (?action=bundle) to make the method more generic like this:

@GET("/api.php")
public void getWhateverAction(@Query("action") String action, Callback<JsonObject> response);

You pass the 'action' as a String argument whenever you call the method.

Problem

Can I download json object as a String without parsing with Retrofit on Android device? When I tried direct approach ``` @GET("/api.php?action=bundle") public void getWholeScheduleString(Callback<String> response); ``` I got error: ``` "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $" ```

Original source

Related problems