Created
May 5, 2019 11:22
-
-
Save tendoasan/5033142a4696a507b711d183b7f2513a to your computer and use it in GitHub Desktop.
[Android-Retrofit]#Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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