試圖上傳圖像,並且它始終只發送字節,而不是圖像文件。這是一個非常簡單的調用,我不需要發送除圖像本身以外的任何參數。我不知道如何格式化日誌,所以我不會在這裏發佈錯誤,除非請求。如何僅使用Retrofit 2.0上傳圖像
服務:
public interface FileUploadService {
@Multipart
@POST("upload_profile_picture")
Call<ResponseBody> uploadProfilePicture(@Part("profile_picture") RequestBody file);
}
呼叫正在作出(一個文件之前生成,不得不因爲這麼需要的崗位主要是words..dumb刪除此代碼..):
// Generate the service from interface
FileUploadService service = ServiceGenerator.createService(FileUploadService.class, this);
// Create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("image/*"), imageFile);
Log.d(LOG_TAG, "formed file");
// finally, execute the request
Call<ResponseBody> call = service.uploadProfilePicture(requestFile);
Log.d(LOG_TAG, "sending call");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,
Response<ResponseBody> response) {
Log.d(LOG_TAG, "success");
Log.d(LOG_TAG, response.toString());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d(LOG_TAG, "failure");
Log.e(LOG_TAG, t.getMessage());
}
});
MediaType.parse方法是否存在問題?我已經嘗試過「multipart/form-data」,「image/jpeg」,以及上面的內容,沒有任何工作。
服務器團隊已表示他們都是接收到的調用,就像字節和圖像文件一樣。
我一直收到400,因爲它發送所有字節。我怎麼才能發送這個?我是否需要以多部分或什麼方式發送?從我所看到的,你只需要用@Body在方法中標記參數,並執行上述操作,它應該都可以工作。有人可以告訴我爲什麼會發生這種情況嗎?謝謝!
我認爲這應該是一個被接受的答案:) – BNK