title: Retrofit2: ClassCastException: java.util.ArrayList cannot be cast to Class
body: <p>I'm using Retrofit2 to try to send an image to the server.  I'm using the command pattern to do so. I am getting the following error: </p>
  
  <blockquote>
    <p>com.gary.test.api.commands.AddMediaCommand$1 cannot be cast to java.util.List</p>
  </blockquote>
  
  <p>So I have class called <code>AddMediaCommand</code></p>
  
  <pre><code>    public class AddMediaCommand implements Commander {
      private final TypedByteArray media;
      private static final int QUALITY = 100;
      private final Context context;
  
      public AddMediaCommand(Context ctx, Bitmap image) {
          this.context = ctx;
  
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          image.compress(Bitmap.CompressFormat.JPEG, QUALITY, out);
  
          media = new TypedByteArray("image/jpeg", out.toByteArray()){
              @Override
              public String fileName() {
                  return "file.jpg";
              }
          };
      }
  
      @Override
      public void execute() {
          new AddMediaService(context, new CommanderListener() {
              @Override
              public void onResultReceived(Bundle extras) {
                  sendBroadcastResult(extras);
              }
          }).addMedia(media);
      }
  
      private void sendBroadcastResult(Bundle extras) {
          Intent intent = new Intent(BroadcastActions.BROADCAST_ADD_MEDIA);
          intent.putExtras(extras);
          LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
      }
  }
  </code></pre>
  
  <p>This executes <code>AddMediaService</code></p>
  
  <pre><code>public class AddMediaService extends BaseService {
  
      private final Context context;
      private final String server;
      private static final String TAG = "AddMediaService";
  
      public AddMediaService(Context context, CommanderListener listener) {
          this.context = context;
          this.server = ApiPreferences.getInstance(context).getDapiDaftServerApi();
          setCommanderListener(listener);
      }
  
      public void addMedia(TypedByteArray image) {
          Call&lt;MediaModel&gt; mdMediaModelCall =  getRetrofit2DapiService(context).addMedia(image);
  
          mdMediaModelCall.enqueue(new retrofit2.Callback&lt;MediaModel&gt;() {
              @Override
              public void onResponse(Call&lt;MediaModel&gt; call, retrofit2.Response&lt;MediaModel&gt; response) {
                  handleSuccess(response.body());
                  Log.d(TAG, "Success");
              }
  
              @Override
              public void onFailure(Call&lt;MDMediaModel&gt; call, Throwable t) {
                  Log.d(TAG, "Failure");
              }
          });
      }
  
      protected void handleSuccess(MediaModel model) {
          Bundle bundle = new Bundle(2);
          bundle.putInt(Extras.RESPONSE_CODE, ResponseCodes.OK);
          bundle.putParcelable(Extras.PARAM_MEDIA, model);
          sendResult(bundle);
      }
  }
  </code></pre>
  
  <p>The <code>addMedia</code> Retrofit2 method is as follows</p>
  
  <pre><code>@retrofit2.http.Multipart
  @retrofit2.http.POST("/media")
  Call&lt;MediaModel&gt; addMedia(@retrofit2.http.Part("file") TypedByteArray image);
  </code></pre>
  
  <p>I am in the process of upgrading from Retrofit 1.9 to Retrofit 2.  This worked without issue in 1.9 so I don't know exactly what this issue is.  I found <a href="https://stackoverflow.com/questions/38940610/retrofit2-classcastexception-java-util-arraylist-cannot-be-cast-to-retrofit2-c">this</a>  which was something similar.  However that OP is casting in their code and I am not.  </p>
  
  <p>If anyone can help me I'd greatly appreciate it.  I have upgraded many of my api calls and there hasn't been this issue.  Any help would be greatly appreciated.</p>
  
  <p><strong>EDIT - MEDIAMODEL CLASS</strong></p>
  
  <pre><code>public class MediaModel implements MediaImage {
  
      //Used to mark a error in the media transfer.
      public static final int NETWORK_ERROR_MEDIA_ID = -100;
  
      Integer id;
      String url;
      int order;
      private String message;
      private Thumbnails thumbnails;
      private transient WeakReference&lt;Bitmap&gt; temporaryImage;
  
      public MediaModel() {
      }
  
      public String getMessage() {
         return message;
      }
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getUrl() {
          return url;
      }
  
      public void setUrl(String url) {
          this.url = url;
      }
  
      public void setOrder(int order) {
          this.order = order;
      }
  
      public Thumbnails getThumbnails() {
          return thumbnails;
      }
  
      public void setTemporaryImage(Bitmap image) {
          if (image == null) {
              if (temporaryImage != null) {
                  temporaryImage.clear();
              }
          } else {
              this.temporaryImage = new WeakReference&lt;&gt;(image);
          }
      }
  
      public Bitmap getTemporaryImage() {
          if (temporaryImage == null) {
              return null;
          } else {
              return temporaryImage.get();
          }
      }
      @Override
      public void setImageUrl(String url) {
          setUrl(url);
      }
  
      @Override
      public String getImageUrl(int imageType) {
          if (imageType == THUMBNAIL_IMAGE_TYPE){
              return getThumbnails().getUrl();
          }
          return getUrl();
      }
  
      public static class Thumbnails {
          private String large;
  
          public Thumbnails(String largeUrl) {
              this.large = largeUrl;
          }
  
          public String getUrl() {
              return large;
          }
  
          public void setUrl(String url) {
              this.large = url;
          }
      }
  
      public static final Creator&lt;MediaModel&gt; CREATOR = new Creator&lt;MediaModel&gt;() {
          public MediaModel createFromParcel(Parcel source) {
              return new MediaModel(source);
          }
  
          public MediaModel[] newArray(int size) {
              return new MediaModel[size];
          }
      };
  
      private MediaModel(Parcel in) {
          id = (Integer) in.readValue(Integer.class.getClassLoader());
          url = in.readString();
          order = in.readInt();
          message = in.readString();
          thumbnails = new Thumbnails(in.readString());
      }
  
      @Override
      public int describeContents() {
          return 0;
      }
  
      @Override
      public void writeToParcel(Parcel dest, int flags) {
          dest.writeValue(id);
          dest.writeString(url);
          dest.writeInt(order);
          dest.writeString(message);
          dest.writeString(thumbnails == null ? EMPTY_STRING : thumbnails.getUrl());
      }
  }
  </code></pre>