安卓:无法使用改装2.0.0 Beta 2中上传多部分图像文件 [英] Android : Unable to upload multipart Image file using Retrofit 2.0.0 beta 2
问题描述
我无法用改装2.0.0-β2上传的图像。
它给我图片丢失的错误。虽然我检查该文件存在于系统中,也是,如果我表现出它是正确显示图像视图。
Hi I am unable to upload an image using retrofit 2.0.0-beta2. It gives me "Image missing" error. Although I checked that file exist in system and also if I show in an Image view it is showing properly.
的URL,它需要上传: http://mywebsite.com /签名/上传/
所以我的BASE_URL是= http://mywebsite.com/
So my BASE_URL is = http://mywebsite.com/
我的 .gradle 文件中有这样的:
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:logging-interceptor:2.6.0'
SeriveGenerator.java 类
public class ServiceGenerator {
public static final String API_BASE_URL = Urls.URL_BASE;
private static OkHttpClient httpClient = new OkHttpClient();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient).build();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.interceptors().add(interceptor);
return retrofit.create(serviceClass);
}
}
UploadService.java 类
public interface UploadService {
@Multipart
@POST("/signature/upload/")
// Tried to change this to "/signature/upload" as well (no slash in end)
// then gives Method not allowed error"
Call<String> upload(
@Part("image") RequestBody file,
@Part("image_name") String name);
}
终于在我的Android活动
And finally in my android activity
// photoFile is the file, checked that it exist in system and
// path is also correct
public void uploadFileToServer() {
UploadService service =
ServiceGenerator.createService(UploadService.class);
String name = "xyz.jpg";
RequestBody requestBody =
RequestBody.create(MediaType.parse("multipart/form-data"), photoFile);
Call<String> call = service.upload(requestBody, name);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response, Retrofit retrofit) {
Log.v("Upload", "success");
}
@Override
public void onFailure(Throwable t) {
Log.e("Upload", t.getMessage());
}
});
}
所以我得到的成功来自活动,但在服务器上文件中记录的成功是不存在的,使用邮差,我试图手动上传的图片中表单数据,是被正确上传
So I am getting success log "success" from activity but on server file is not there, using Postman, I tried manually upload the image in form data, is gets uploaded correctly.
推荐答案
我前几天遇到了类似的问题。这里说到我的工作code。
I faced a similar problem a few days ago. Here comes my working code.
首先,你必须定义接口。不添加的内容类型头是非常重要的。
在下面可以看到 @part(图片\\;
图片是对应的名称,后端预期(对我来说),所以你不得不这样做。更改此名称,使其与您的后端工作。文件名= \\图片\\只是定义了你上传的图片的名称。
在我的情况的方法是在DiaryService.java。
At first you have to define the Interface. It is very important NOT to add a content type header.
In the following you see the @Part("picture\";
the "picture" is the corresponding name, that the Backend expects (in my case). So you have to change this name to make it work with your Backend. The filename=\"image\" just defines the name of your uploaded image.
In my case the method is in the "DiaryService.java".
@Multipart
@POST("signature/upload")
public Call<Void> addImageElementToDiary(@Part("picture\"; filename=\"image\" ") RequestBody picture,
@Part("sort") RequestBody sort);
现在您可以拨打上述定义的服务。重要的是要正确分析的MediaType是非常重要的。你用的multipart / form-data的,这并没有我的情况下工作。您应该在以下Mediatypes看看。
Now you can call the above defined Service. It is important to parse the Mediatype correctly. You used "multipart/form-data", this did not work in my case. You should take a look at the following Mediatypes.
File file = new File(((CreateDiaryImage) element).getImagePath());
RequestBody sort = RequestBody.create(MediaType.parse("text/plain"), element.getSort() + "");
RequestBody requestBody =
RequestBody.create(MediaType.parse("image/*"), file);
Call<Void> call = diaryService.addImageElementToDiary(requestBody, sort);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Response<Void> response, Retrofit retrofit) {
Log.v("Upload", "success");
}
@Override
public void onFailure(Throwable t) {
Log.e("Upload", t.getMessage());
}
});
这篇关于安卓:无法使用改装2.0.0 Beta 2中上传多部分图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!