您好,我无法使用改造 2.0.0-beta2 上传图像。 它给我“图像丢失”错误。尽管我检查过该文件存在于系统中,而且如果我在 ImageView 中显示它也能正常显示。
需要上传的URL: http://mywebsite.com/signature/upload/
所以我的 BASE_URL 是 = 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 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());
}
});
}
因此,我从 Activity 中获得成功日志“成功”,但服务器文件不存在,使用 Postman,我尝试手动上传表单数据中的图像,但已正确上传。
最佳答案
几天前我遇到了类似的问题。这是我的工作代码。
首先你必须定义接口(interface)。不要添加内容类型 header 非常重要。
在下面你会看到 @Part("picture\";
"picture"是相应的名称,后端期望(在我的例子中)。所以你必须更改这个名称才能使它与您的后端一起工作。filename=\"image\"只是定义了您上传的图像的名称。
在我的例子中,方法在“DiaryService.java”中。
@Multipart
@POST("signature/upload")
public Call<Void> addImageElementToDiary(@Part("picture\"; filename=\"image\" ") RequestBody picture,
@Part("sort") RequestBody sort);
现在你可以调用上面定义的服务了。正确解析 Mediatype 很重要。您使用了“multipart/form-data”,这对我来说不起作用。您应该查看以下媒体类型。
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());
}
});
关于安卓 : Unable to upload multipart Image file using Retrofit 2. 0.0 测试版 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34869807/