Upgrade how to handle answers to two types?

I have an API server for an Android app.

I am trying to use Retrofit for this.

Api server can return normal, say /users

 { "userId":"123", "username":"John Doe" } 

but it can also return an error.

 { "errorCode":0, "errorMessage":"blah" } 

So I'm just wondering if there is a way to handle errors globally for each method in Retrofit?

Edit:

I ended up with a server modification.

 { data:[], error:{} } 
+6
source share
1 answer

make api method using JsonObject as below

  Call<JsonObject> listRes(@Body GsonRegisterUser user); 

and now used

 Call<JsonObject> call = listenerRegisterUser.listRes(gsonRegisterUser); 

You will now get a json response using the following method, and you can analyze as per your requirement

 @Override public void onResponse(Response<JsonObject> response) { Log.e(TAG,response.body().toString()); } 
+1
source

Source: https://habr.com/ru/post/989987/


All Articles