我正在尝试使用Retrofit从我的API获取错误消息,我尝试做的是以下操作,但是我遇到一个导致我的应用崩溃的错误,这是错误日志:

    --------- 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错误消息:

[
  {
    "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;
    }
}


主要活动:

 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());


     }
});

最佳答案

您需要转换为List<ApiError>,因为这是您收到的内容:

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


转换为单个ApiError。要转换为列表,您需要适当的转换器。由于type erasure,这有点棘手

您需要像这样从Type获得的TypeToken

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


还有几点


error.toString(),因为error现在是List,对您没有多大帮助。您可以改用TextUtils.join("\n", error)
return (List<ApiError>) new ApiError();没有任何意义。您不能在这样的对象列表中转换对象。 return new ArrayList<>();最有可能满足您的需求。

10-08 18:04