多部分改造 1.9 -> 2
Multipart Retrofit 1.9 -> 2
我曾经用 Retrofit 1.9:
发送 POST 请求
TypedFile typedFile = picture != null ? new TypedFile("image/*", tempFile) : null;
@Multipart
@POST("/goals")
Observable<Goal> postGoal(
@Part("name") String name,
@Part("picture") TypedFile picture
);
我转移到 retrofit 2.0.2,但我无法使其正常工作。根据 https://github.com/square/retrofit/issues/1140 我使用了:
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), name);
RequestBody picture = RequestBody.create(MediaType.parse("image/*"), tempFile);
@Multipart
@POST("goals")
Observable<Goal> postGoal(
@Part("name") RequestBody name,
@Part("picture\"; filename=\"temp.png\" ") RequestBody picture
);
但似乎请求在没有文件(照片)的情况下进行。怎么了?
要使用 retrofit 2.0 上传图片,您可以使用 MultipartBody.Part 作为图片参数。希望此解决方案能解决您的问题。
您的API声明
@Multipart
@POST("/goals")
Observable<Goal> postGoal(@Part("name") String name, @Part MultipartBody.Part imageFile);
您的 api 电话
RequestBody imageName = RequestBody.create(MediaType.parse("text/plain"), name);
//prepare image file
File file = new File(imagePath);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part imageFileBody = MultipartBody.Part.createFormData("picture", file.getName(), requestBody);
YourAPI service = retrofit.create(YourAPI.class);
Call<Goal> call = service.postGoal(imageName, imageFileBody);
call.enqueue(new Callback<Goal>() {
@Override
public void onResponse(Call<Goal> call, Response<Goal> response) {
//handle success
}
@Override
public void onFailure(Call<Goal> call, Throwable t) {
}
});
我曾经用 Retrofit 1.9:
发送 POST 请求TypedFile typedFile = picture != null ? new TypedFile("image/*", tempFile) : null;
@Multipart
@POST("/goals")
Observable<Goal> postGoal(
@Part("name") String name,
@Part("picture") TypedFile picture
);
我转移到 retrofit 2.0.2,但我无法使其正常工作。根据 https://github.com/square/retrofit/issues/1140 我使用了:
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), name);
RequestBody picture = RequestBody.create(MediaType.parse("image/*"), tempFile);
@Multipart
@POST("goals")
Observable<Goal> postGoal(
@Part("name") RequestBody name,
@Part("picture\"; filename=\"temp.png\" ") RequestBody picture
);
但似乎请求在没有文件(照片)的情况下进行。怎么了?
要使用 retrofit 2.0 上传图片,您可以使用 MultipartBody.Part 作为图片参数。希望此解决方案能解决您的问题。
您的API声明
@Multipart
@POST("/goals")
Observable<Goal> postGoal(@Part("name") String name, @Part MultipartBody.Part imageFile);
您的 api 电话
RequestBody imageName = RequestBody.create(MediaType.parse("text/plain"), name);
//prepare image file
File file = new File(imagePath);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part imageFileBody = MultipartBody.Part.createFormData("picture", file.getName(), requestBody);
YourAPI service = retrofit.create(YourAPI.class);
Call<Goal> call = service.postGoal(imageName, imageFileBody);
call.enqueue(new Callback<Goal>() {
@Override
public void onResponse(Call<Goal> call, Response<Goal> response) {
//handle success
}
@Override
public void onFailure(Call<Goal> call, Throwable t) {
}
});