AND4011002849 AND4011002849 - 2 months ago 25
Android Question

custom message error using retrofit ResponseBody

I'm trying to get the error message from my API using

Retrofit
, what I tried to do is the following, but I'm getting an error which causes my app to crash, this is the error log:

--------- beginning of crash
10-03 16:51:56.776 18801-18801/com.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app, PID: 18801
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
at com.app.rest.ErrorUtils.parseError(ErrorUtils.java:25)
at com.app.MainActivity$1$1.onResponse(MainActivity.java:77)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37) 
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25) 
at com.app.rest.ErrorUtils.parseError(ErrorUtils.java:25) 
at com.app.MainActivity$1$1.onResponse(MainActivity.java:77) 
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) 
at android.os.Handler.handleCallback(Handler.java:746) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 


API error message:

[
{
"field": "username",
"message": "\"Username\" não pode ficar em branco."
},
{
"field": "password",
"message": "Invalid username or password."
}
]


ErrorUtils:

public class ErrorUtils {

public static List<ApiError> parseError(Response<?> response) {

Converter<ResponseBody, List<ApiError>> converter = ApiClient.getClient()
.responseBodyConverter(ApiError.class, new Annotation[0]);

List<ApiError> error;

try {
error = converter.convert(response.errorBody());
} catch (IOException e) {
e.printStackTrace();
return (List<ApiError>) new ApiError();
}

return error;
}
}


ApiError:

public class ApiError {

@SerializedName("message")
private String message;

public ApiError() {
}


public String message() {
return message;
}
}


MainActivity:

Call<UserResponse> call = apiService.createUser(user);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
int statuscode = response.code();
if (statuscode == 200) {
Intent mIntent = new Intent(MainActivity.this, NavigationMain.class);
startActivity(mIntent);
finish();
} else {
Log.d("Login_call", response.code() + "");
List<ApiError> error = ErrorUtils.parseError(response);
Log.d("Login_call_error", error.toString() + "");
}

}

@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
Log.v(TAG, t.getMessage());


}
});

Answer

You need to convert to a List<ApiError> since that is what you receive:

Converter<ResponseBody, ApiError> converter = ApiClient.getClient()
            .responseBodyConverter(ApiError.class, new Annotation[0]);

converts into a single ApiError. To convert to a list, you need to proper converter. This is a little tricky because of type erasure

You need a Type that you obtain from a TypeToken like so:

Type type = new TypeToken<List<ApiError>>() {}.getType();
Converter<ResponseBody, List<ApiError>> converter = ApiClient.getClient()
            .responseBodyConverter(type, new Annotation[0]);

A few more points

  • error.toString(), because error is now a List, will not help you much. You can use TextUtils.join("\n", error) instead.
  • return (List<ApiError>) new ApiError(); makes no sense. You can't convert an object in a list of object like that. Most likely, return new ArrayList<>(); is sufficient for what you need.