title: How to use gson TypeAdapter in Retrofit 2?
body: <p>I have a working code wherein my retrofit client can retrieve a List of objects(country) from an api. The problem is, the parameter used by the api returns an ARRAY if I used to retrieve all the countries then it returns a single OBJECT when I want to query for a single country. As a result the following exception is shown.</p>
  
  <pre><code>java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 4 column 17 path $.RestResponse.result
  </code></pre>
  
  <p>By the way, I'm using Retrofit 2.</p>
  
  <p>I tried the accepted answer in this thread
  <a href="https://stackoverflow.com/questions/26500772/how-to-handle-parameters-that-can-be-an-array-or-object-in-retrofit-on-android">How to handle parameters that can be an ARRAY or OBJECT in Retrofit on Android?</a></p>
  
  <p>But still it won't work. Here is my implementation.</p>
  
  <p>API</p>
  
  <pre><code>http://services.groupkt.com/country/get/all
  http://services.groupkt.com/country/get/iso2code/{alpha2_code}
  </code></pre>
  
  <p>ApiInterface</p>
  
  <pre><code>public interface ApiInterface {
  
      @GET("country/get/all")
      Call&lt;Example&gt; getCountry();
  
      @GET("country/get/iso2code/{alpha2_code}")
      Call&lt;Example&gt; searchCountryByIso2Code(@Path("alpha2_code") String alpha2Code);
  
      @GET("country/get/iso3code/{alpha3_code}")
      Call&lt;Example&gt; searchCountryByIso3Code(@Path("alpha3_code") String alpha3Code);
  
      @GET("country/search?text={text to search}")
      Call&lt;Example&gt; searchCountry(@Path("text to search") String searchText);
  }
  </code></pre>
  
  <p>CountryTypeAdapter</p>
  
  <pre><code>public class CountryTypeAdapter extends TypeAdapter&lt;RestResponse&gt; {
  
      private Gson mGson = new Gson();
  
      @Override
      public void write(JsonWriter jsonWriter, RestResponse restResponse) throws IOException {
          mGson.toJson(restResponse, RestResponse.class, jsonWriter);
      }
  
      @Override
      public RestResponse read(JsonReader jsonReader) throws IOException {
          RestResponse result;
  
          jsonReader.beginObject();
          jsonReader.nextName();
  
          if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) {
              result = new RestResponse((Result[]) mGson.fromJson(jsonReader, Result.class));
          } else if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
              result = new RestResponse((Result) mGson.fromJson(jsonReader, Result.class));
          } else {
              throw new JsonParseException("Unexpected token " + jsonReader.peek());
          }
  
          jsonReader.endObject();
          return result;
      }
  }
  </code></pre>
  
  <p>ApiClient</p>
  
  <pre><code>public class ApiClient {
  
      public static final String BASE_URL = "http://services.groupkt.com/";
      private static Retrofit mRetrofit;
  
      public static Retrofit getmRetrofitClient(){
  
          if (mRetrofit == null) {
              mRetrofit = new Retrofit.Builder()
                      .baseUrl(BASE_URL)
                      .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().registerTypeAdapter(CountryTypeAdapter.class, new CountryTypeAdapter()).create()))
                      .build();
          }
          return mRetrofit;
      }
  
  }
  </code></pre>
  
  <p>EDIT:
  RestResponse</p>
  
  <pre><code>public class RestResponse {
  
      @SerializedName("messages")
      @Expose
      private List&lt;String&gt; messages = null;
      @SerializedName("result")
      @Expose
      private List&lt;Result&gt; result = null;
  
      public RestResponse() {
      }
  
      public RestResponse(List&lt;String&gt; messages, List&lt;Result&gt; result) {
          this.messages = messages;
          this.result = result;
      }
  
      public RestResponse(Result... result) {
          this.result = Arrays.asList(result);
      }
  
      public List&lt;String&gt; getMessages() {
          return messages;
      }
  
      public void setMessages(List&lt;String&gt; messages) {
          this.messages = messages;
      }
  
      public List&lt;Result&gt; getResult() {
          return result;
      }
  
      public void setResult(List&lt;Result&gt; result) {
          this.result = result;
      }
  
  
  }
  </code></pre>