无法使用Retrofit上传视频,可能的修复?

时间:2016-12-20 07:14:28

标签: java android retrofit multipartform-data retrofit2

我正在尝试使用Retrofit作为多部分表单数据将视频上传到服务器,而我正在使用Retrofit版本:2.02

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

这是API客户端类的代码:

ApiClient.java

    public class ApiClient {

    public static final String BASE_URL = "https://api.sproutvideo.com";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

这是API接口的代码:

ApiInterface.java

    public interface ApiInterface {
    @Multipart
    @POST("/v1/videos")
    Call<SproutReply> pushVideo (@Header("SproutVideo-Api-Key") String key, @Part("file\"; filename=\"pp.mp4") RequestBody file);
}

我的要求是上传使用Intent.ACTION_PICK挑选的视频。

这就是我调用API的方式:

 private void makeRequest(Uri uri)
{


   try {
       String path = uri.toString();
       Log.e("PATH", path);
       URI ss = new URI(uri.toString());
       file = new File(ss);

   }
   catch (Exception e){}


    RequestBody video = RequestBody.create(MediaType.parse("video/mp4"),file);
    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    Call<SproutReply> call = apiService.pushVideo(KEY,video);
    call.enqueue(new Callback<SproutReply>()
    {
        @Override
        public void onResponse(Call<SproutReply> call, Response<SproutReply> response)
        {
            try
            {
                Log.e("TAG",""+response.body().toString());




            }
            catch (Exception e)
            {
                Toast.makeText(getActivity(), "Check data connection", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<SproutReply> call, Throwable t)
        {
            // Log error here since request failed
            Log.e("FAILURE", t.toString());
        }
    });


}

此次调用会在此行生成NullPointerException content == null

RequestBody video = RequestBody.create(MediaType.parse("video/mp4"),file);

但是在这行中,当我将Uri(Log.e("PATH", path);)记录为String时,我得到的值为:

  

内容://com.android.providers.media.documents/document/video%3A75406

使用这个Uri我也可以在VideoView中播放视频,但是在Retrofit中它似乎崩溃了,可能是什么导致了这个以及如何修复?

以下是logcat:

java.lang.NullPointerException: content == null
                                                             at okhttp3.RequestBody.create(RequestBody.java:103)
                                                             at com.example.demovid.UploadFragment.makeRequest(UploadFragment.java:91)
                                                             at com.example.demovid.UploadFragment.onEvent(UploadFragment.java:133)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:485)
                                                             at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:416)
                                                             at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:397)
                                                             at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:370)
                                                             at org.greenrobot.eventbus.EventBus.post(EventBus.java:251)
                                                             at com.example.demovid.MainActivity.onActivityResult(MainActivity.java:67)
                                                             at android.app.Activity.dispatchActivityResult(Activity.java:6456)
                                                             at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
                                                             at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
                                                             at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                             at android.os.Looper.loop(Looper.java:148)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:2)

问题是您有内容URI而不是文件路径。当您将其传递给File构造函数时,它会抛出一个您默默忽略的FileNotFoundException

`catch (Exception e){}`

您可以使用 -

获取数据的文件描述符,而不是使用文件
ParcelFileDescriptor fd = getActivity().getContentResolver().openFileDescriptor(uri, "r");

并使用它来构建您的RequestBody。没有开箱即用的方法来从描述符创建一个,但编写一个例程很容易 -

public static RequestBody createBody(final MediaType contentType, final ParcelFileDescriptor fd) {
    if (fd == null) throw new NullPointerException("content == null");

    return new RequestBody() {
        @Override public MediaType contentType() {
            return contentType;
        }

        @Override public long contentLength() {
            return fd.getStatSize();
        }

        @Override public void writeTo(BufferedSink sink) throws IOException {
            Source source = null;
            try {
                source = Okio.source(new ParcelFileDescriptor.AutoCloseInputStream(fd));
                sink.writeAll(source);
            } finally {
                Util.closeQuietly(source);
            }
        }
    };
}

然后像 -

一样使用它
RequestBody video = RequestBody.createBody(MediaType.parse("video/mp4"), fd);

我还注意到您正在对媒体类型进行硬编码,您也可以通过调用getType来获取内容解析器中的类型