如何使用Retrofit2,Gson和Rx处理多种可能的响应类型

时间:2017-01-17 09:12:45

标签: retrofit2

API我必须使用sucks,并且总是返回HTTP 200.但有时会有正确的响应:

[{"blah": "blah"}, {"blah": "blah"}]

有时会出现错误:

{"error": "Something went wrong", "code": 123}

我正在使用带有Gson转换器和Rx适配器的Retrofit2:

final Api api = new Retrofit.Builder()
        .baseUrl(URL)
        .client(client)
        .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(Api.class);

现在,当我收到错误响应时,调用onError处理程序时会出现以下异常:

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
    at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
    at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
    at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
    at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
    at rx.Subscriber.setProducer(Subscriber.java:211)
    at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76)
    at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
    at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
    at rx.Observable.unsafeSubscribe(Observable.java:10144)
    at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
    at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
    at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)

我该如何解决?如果我可以在onError处理程序中获得响应,我可以使用正确的错误模型类重新解析它。但似乎我无法得到原始的回应。

1 个答案:

答案 0 :(得分:3)

您可以使用自定义Gson反序列化器将两个响应编组为单个对象类型。下面是假设您当前的响应类型为List<Map<String, String>>的想法的草图,您需要根据实际的返回类型进行调整。我也假设API总是在成功时返回一个数组 -

public class MyResponse {
  String error;
  Integer code;
  List<Map<String, String>> response;
}

interface MyApi {
  @GET("/")
  Observable<MyResponse> myCall();
}

private class MyResponseDeserializer implements JsonDeserializer<MyResponse> {
  public MyResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {
    MyResponse response = new MyResponse();
    if (json.isJsonArray()) {
      // It is an array, parse the data
      Type responseType = new TypeToken<List<Map<String, String>>>(){}.getType();
      response.response = context.deserialize(json, responseType);
    } else {
      // Not an array, parse out the error info
      JsonObject object = json.getAsJsonObject();
      response.code = object.getAsJsonPrimitive("code").getAsInt();
      response.error = object.getAsJsonPrimitive("error").getAsString();
    }
    return response;
  }
}

使用以上内容创建自定义Gson

Gson gson = new GsonBuilder()
    .registerTypeAdapter(MyResponse.class, new MyResponseDeserializer())
    .create();

在改装生成器中使用它 -

.addConverterFactory(GsonConverterFactory.create(gson))

您还应该更新界面以返回Observable<MyResponse>。您现在可以在onNext中获得成功和错误。您需要检查对象以确定它是否是成功的响应(response != null)。