我有自定义的TypeAdapterFactory,它在空列表

时间:2017-09-23 09:20:03

标签: java android

public class SignInResponse {

@SerializedName("Status")
private String status;
@SerializedName("personID")
public long personID;
@SerializedName("UserInfo")
public SignInResponseUser userInfo;
@SerializedName("Types")
public ArrayList<SignInResponseTypes> TYpes;
}

当获取响应时,如果(类型)是数组列表为空,则应用程序崩溃。当空数组列表出现时,我可以处理(返回delegate.fromJsonTree(jsonElement)中的响应;

我的自定义适配器:

public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
    final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);

    return new TypeAdapter<T>() {

        public void write(JsonWriter out, T value) throws IOException {
            delegate.write(out, value);
        }

        public T read(JsonReader in) throws IOException {
            JsonElement jsonElement = elementAdapter.read(in);
            if (!jsonElement.isJsonNull() && (jsonElement.isJsonObject())) {
                JsonObject jsonObject = jsonElement.getAsJsonObject();
                if (jsonObject.has("Types") && (jsonObject.get("Types").isJsonArray())) {
                    JsonArray jsonTypeArray = jsonObject.get("Types").getAsJsonArray();
                    for (int i = 0; i < jsonTypeArray.size(); i++) {
                        JsonObject jsonType = jsonTypeArray.get(i).getAsJsonObject();
                        for (int t = 0; t < jsonType.size(); t++) {
                            if (jsonType.has("Type")) {
                                String annotationValue = jsonType.get("Type").getAsString();
                                String[] splitTypeName = annotationValue.split("\\|");
                                jsonType.addProperty("enterName", splitTypeName[0]);
                                jsonType.addProperty("typeName", splitTypeName[1]);
                            }
                        }
                    }
                }
                jsonElement = jsonObject;
            }
            return delegate.fromJsonTree(jsonElement);
        }
    }.nullSafe();
}

crashReport:  致命异常:AsyncTask#2                                                                                   过程:组织...,PID:20561                                                                                   com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但在路径$处为STRING                                                                                       在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:224)                                                                                       在com.google.gson.TypeAdapter.fromJsonTree(TypeAdapter.java:285)                                                                                       at com..model.user.repository.network.response.deserialize.SignInTypeAdapter $ 1.read(SignInTypeAdapter.java:48)                                                                                       在com.google.gson.TypeAdapter $ 1.read(TypeAdapter.java:199)                                                                                       at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)                                                                                       at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)                                                                                       at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)                                                                                       at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)                                                                                       at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)                                                                                       at retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:91)

崩溃的反应:

{ “类型”: “”, “的UserInfo”: “”, “状态”:2002}

类型为空,响应等待该字段中的数组列表(类型)

任何解决方案;

0 个答案:

没有答案