Skip to content

Instantly share code, notes, and snippets.

@tendoasan
Created May 5, 2019 11:22
Show Gist options
  • Save tendoasan/5033142a4696a507b711d183b7f2513a to your computer and use it in GitHub Desktop.
Save tendoasan/5033142a4696a507b711d183b7f2513a to your computer and use it in GitHub Desktop.
[Android-Retrofit]#Android
public class Upload {
public interface UserService {
@Multipart
@POST("user/uploadavatar")
Observable<ResponseBody> uploadAvatar(
@Part("name") RequestBody name,
@Part MultipartBody.Part file,
);
}
public static void avatarUpload(String filePath, Observer<ResponseBody> observer){
File file = new File(filePath);
if (!file.exists()){
Log.e(TAG, "pictureUpload: filePath = " + filePath + ",该文件不存在");
return;
}
RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), file.getName());
RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), fileRequestBody);
UserService()
.uploadAvatar(name, filePart)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment