我试图使用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中,它似乎崩溃了,是什么导致了这个问题,以及如何修复呢?
这也是逻辑猫:
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)发布于 2016-12-20 21:22:40
问题是您有一个内容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从内容解析器获得该类型。
https://stackoverflow.com/questions/41236736
复制相似问题