Retrofit1文件上传

来源:互联网 发布:找网络水军公司 编辑:程序博客网 时间:2024/06/01 10:51

仅仅是想要上传个图片,在百度上找了很久,基本都是讲retrofit2的文件上传方法,少数将retrofit1的几篇方法试过却上传不成功,最终最一篇英文文章中找到了方法,亲测可行。一下为关键部分代码。

public interface UserApi{
@Multipart
@POST("/modify")Observable<UserEntity> modify_user_portrait(@Part("portrait") TypedFile portrait, @Part("user_id") int userId);
}
这里采用Retrofit+Rx方式,关于Rx编程本人也是刚刚接触,只是略懂点皮毛,在这不做说明。

public Observable<UserEntity> modifyUserPortrait(int userId, File portrait) {    TypedFile typedFile = new TypedFile("multipart/form-data", portrait);    return getService().modify_user_portrait(typedFile, userId);}
传入需要上传的文件及其他信息。

0 0