title: How to handle multiple possible response types with Retrofit2, Gson and Rx
body: <p>The API i have to use sucks, and always returns HTTP 200. But sometimes there is proper response:</p>
  
  <pre class="lang-json prettyprint-override"><code>[{"blah": "blah"}, {"blah": "blah"}]
  </code></pre>
  
  <p>and sometimes, there is error:</p>
  
  <pre class="lang-json prettyprint-override"><code>{"error": "Something went wrong", "code": 123}
  </code></pre>
  
  <p>I'm using Retrofit2 with Gson converter and Rx adapter:</p>
  
  <pre class="lang-java prettyprint-override"><code>final Api api = new Retrofit.Builder()
          .baseUrl(URL)
          .client(client)
          .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
          .addConverterFactory(GsonConverterFactory.create())
          .build()
          .create(Api.class);
  </code></pre>
  
  <p>And now, when I receive error response, the <code>onError</code> handler is called with following exception:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>How can I solve it? If I could get the response in the <code>onError</code> handler, I could reparse it with proper error model class. But it seems I can't get the raw response.</p>