【发布时间】:2017-05-05 08:09:21
【问题描述】:
我正在尝试使用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);) 记录为字符串时,我获得的值为:
content://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)
【问题讨论】:
标签: java android retrofit multipartform-data retrofit2