Retrofit2:ClassCastException:java.util.ArrayList无法强制转换为Class

时间:2016-12-29 12:15:51

标签: android retrofit retrofit2 classcastexception

我正在使用Retrofit2尝试将图像发送到服务器。我正在使用命令模式这样做。我收到以下错误:

  

com.gary.test.api.commands.AddMediaCommand $ 1无法强制转换为java.util.List

所以我有一个名为AddMediaCommand

的课程
    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);
    }
}

执行AddMediaService

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<MediaModel> mdMediaModelCall =  getRetrofit2DapiService(context).addMedia(image);

        mdMediaModelCall.enqueue(new retrofit2.Callback<MediaModel>() {
            @Override
            public void onResponse(Call<MediaModel> call, retrofit2.Response<MediaModel> response) {
                handleSuccess(response.body());
                Log.d(TAG, "Success");
            }

            @Override
            public void onFailure(Call<MDMediaModel> 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);
    }
}

addMedia Retrofit2方法如下

@retrofit2.http.Multipart
@retrofit2.http.POST("/media")
Call<MediaModel> addMedia(@retrofit2.http.Part("file") TypedByteArray image);

我正在从Retrofit 1.9升级到Retrofit 2.这在1.9中没有问题,所以我不知道究竟是什么问题。我发现this类似的东西。然而,OP正在他们的代码中投射而我不是。

如果有人能帮助我,我会非常感激。我升级了很多api电话,但没有这个问题。任何帮助将不胜感激。

编辑 - MEDIAMODEL CLASS

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<Bitmap> 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<>(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<MediaModel> CREATOR = new Creator<MediaModel>() {
        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());
    }
}

1 个答案:

答案 0 :(得分:0)

在AddMediaCommand中创建一个ResquestBody对象:

File file = new File("file.jpg");
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

然后在AddMediaService中的addMedia方法中使用:

创建MultipartBody对象

MultipartBody.Part.createFormData("file", file.getName(), requestFile)

最后修改Retrofit webservice接口以使用MultipartBody.Part对象:

@retrofit2.http.Multipart
@retrofit2.http.POST("/media")
Call<MediaModel> addMedia(@retrofit2.http.Part("file") MultipartBody.Part image);

您可以在此处找到更多信息https://github.com/square/retrofit/issues/1063