我尝试使用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;
}
}
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());
}
});
答案 0 :(得分:1)
您需要转换为List<ApiError>
,因为这是您收到的内容:
Converter<ResponseBody, ApiError> converter = ApiClient.getClient()
.responseBodyConverter(ApiError.class, new Annotation[0]);
转换为单个ApiError
。要转换为列表,您需要正确的转换器。由于type erasure
您需要Type
获得的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<>();
毫无意义。您无法像这样转换对象列表中的对象。最有可能的是,<add name="DB" connectionString="Data Source=...;
Integrated Security=False;User ID=sql;Password=xxx" providerName="System.Data.SqlClient"/>
足以满足您的需求。