13

i have problem to use model class in retrofit lib. A backend side field name has changed.

Is it possible to get response without model class?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Vadivel
  • 780
  • 1
  • 6
  • 21
  • 1
    refer http://stackoverflow.com/questions/37119905/retrofit-2-without-model-class – sasikumar Nov 09 '16 at 09:18
  • 4
    If your backend is not reliable, then Retrofit may be the wrong library. Just stick with OkHttp – OneCricketeer Nov 09 '16 at 09:23
  • Question doesn't make sens ... *Retrofit - **Type-safe** HTTP client for Android and Java by Square, Inc.* ... *Is it possible to get response without model class?* **then what you wana get at the end?** – Selvin Nov 09 '16 at 09:24
  • 3
    @Selvin OP probably means just get raw strings, not something that will need to be mapped to a user defined POJO class – OneCricketeer Nov 09 '16 at 09:27
  • 2
    ... or maybe some dictionaries(HashMap) ... but as you wrote - why Retrofit then? ... still *OP probably means just get raw strings* ... well without posting this in the question it's only blind guess – Selvin Nov 09 '16 at 09:28

5 Answers5

19

Yes, you can.

@POST("url")

 Call<JsonObject> register(@Query("name") String name,

                           @Query("password") String password);

Just write JsonArray or JsonObject according to your response instead of Model class.

Then, get data from JsonObject or JsonArray which you get in response as below

Call<JsonObject> call = application.getServiceLink().register();

call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                JsonObject object = response.body();
                //parse object 
            }

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

            }
        });
chetan
  • 681
  • 4
  • 21
4

It is perfectly possible using different return values. I assume that you currently use Gson to deserialize JSON responses, and they get converted to the actual class. However, you may choose to convert the returned response to JsonElement (or some more specific JSON class), in that case you will get a JSON item which you can manipulate as you wish to. Something like:

@GET("url")
Call<JsonElement> apiCall();
Malcolm
  • 41,014
  • 11
  • 68
  • 91
3

@Vadivel here it is the version of GET based on @chetan work:

@GET("http://your.api.url")
Call<JsonObject> getGenericJSON();

Call<JsonObject> call = api.getGenericJSON();
call.enqueue(new Callback<JsonObject>() {

    @Override
    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
        if (response.isSuccessful() && response.body() != null) {
            Log.d(TAG, response.body().toString());
        } else {
            Log.e(TAG, "Error in getGenericJson:" + response.code() + " " + response.message());
        }
    }

    @Override
    public void onFailure(Call<JsonObject> call, Throwable t) {
        Log.e(TAG, "Response Error");
    }
});
Scognito
  • 152
  • 1
  • 13
2

Is it possible to get response without model class

If I get you right - sure. You do not have to make response JSON auto-converted. You can do this easily by hand if needed, by retrieving raw response. Once you got that you can do whatever you need.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

You can simply "rename" field on your side using

@SerializedName("newFieldName")
SomeClass oldField;