简体   繁体   English

使用Retrofit2上传视频

[英]Upload video using retrofit2

I am trying to upload a video from android device to server using Retrofit2 and end up getting error '400 Bad Request'. 我正在尝试使用Retrofit2将视频从android设备上传到服务器,并最终收到错误“ 400 Bad Request”。 Below is the implementation. 下面是实现。 Could somebody help to fix the error? 有人可以帮助纠正错误吗?

public interface RetrofitService {
/**
 * Upload Videos to Server
 */
@Multipart
@POST("store/S3")
Call<ResponseBody> uploadToServer(@Query("key") String ServerAPI,
                                      @Query("mimetype") String videoMimeType,
                                      @Query("path") String path,
                                      @Query("container") String container,
                                      @Query("policy") String policy,
                                      @Query("signature") String signature,
                                      @Part MultipartBody.Part video,
                                      @Part("type") String videoType,
                                      @Part("name") String videoName );

} }

Client implemetation in helper.java helper.java中的客户端实现

  private void uploadVideos(String videUri, String policy, String signature){
    String BASE_URL = "https://www.example.com/api/";
    String EXAMPLE_API_KEY = "xebfc";
    String mimeType = "video/mp4";
    String path = "mezzanine_videos/";
    String container = S3_BUCKET;

    // use the FileUtils to get the actual file by uri
    File videoFile = new File(videoUri);

    // create RequestBody instance from file
    RequestBody videoBody = RequestBody.create(MediaType.parse("video/*"), videoFile);

    // MultipartBody.Part is used to send the actual file
    MultipartBody.Part vFile = MultipartBody.Part.createFormData("video", videoFile.getName(), videoBody);

    String videoType = "video/mp4";
    String videoName = "video.mp4";

    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();

    RetrofitService service = retrofit.create(RetrofitService.class);
    Call<ResponseBody> call = service.uploadToServer( EXAMPLE_API_KEY, mimeType, path, container, policy, signature, vFile, videoType, videoName);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.d("Response", "Successful Response");
        }

        @Override
        public void onFailure(Call call, Throwable t) {
            Log.d("Response", "Failure Response");
        }
    }); }

Server implementation works fine because I get a valid response by doing a curl post like below. 服务器实现工作正常,因为我通过执行如下所示的curl发布来获得有效的响应。

curl -X POST -F fileUpload=@animation.mov "https://www.example.com/api/store/S3?key= xebfc&mimetype=video%2Fmp4&path=mezzanine_videos/&container= S3_BUCKET&policy=ppppp&signature=ssss

After some debugging on MultipartBody.Part.createFormData, figured out that the name was not matching with my backend implementation. 在对MultipartBody.Part.createFormData进行一些调试之后,发现该名称与我的后端实现不匹配。 Below code fixed the issue. 下面的代码解决了该问题。

    MultipartBody.Part vFile = MultipartBody.Part.createFormData("fileUpload", videoFile.getName(), fileBody);

Rest of the code is same as mentioned above. 其余代码与上述相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM