在Android中使用Okhttp时,文件上传的限制大小(mp4视频)是多少?

时间:2016-02-07 22:22:27

标签: android file-upload http-post multipartform-data okhttp

我正在尝试将视频文件上传到Ruby on Rails后端。我正在使用OkHttp。

上传较少的MB文件时效果很好。但是对于大文件,它无法正常工作并进入此块

@Override
public void onFailure(Request request, IOException e) {
// Handle the error
Log.d(TAG, "video upload request failed" + request.body().toString() + "error = " + e.getMessage());
}

好奇的是打印时出现错误

我已经尝试使用相同的API使用POSTMAN上传60 MB文件,并且它工作得非常好。所以服务器端没有问题。]

以下是供参考的代码:

 private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("video/mp4");

public static boolean uploadVideoOnServer(final Context mContext, final Video video) {

    final OkHttpClient client = new OkHttpClient();
    client.setConnectionPool(new ConnectionPool(0, 60000));

    String url = Constants.URL_MEMORY_UPLOAD + TJPreferences.getActiveJourneyId(mContext) + "/videos";

    try {
        RequestBody requestBody = new MultipartBuilder()
                .type(MultipartBuilder.FORM)
                .addFormDataPart("video[video_file]", "file",
                        RequestBody.create(MEDIA_TYPE_PNG, new File(video.getVidLocalUrl())))
                .addFormDataPart("video[user_id]", video.getCreatedBy().getIdOnServer())
                .addFormDataPart("api_key", TJPreferences.getApiKey(mContext))
                .addFormDataPart("video[latitude]", String.valueOf(video.getPlaceTaken().getLatitude()))
                .addFormDataPart("video[longitude]", String.valueOf(video.getPlaceTaken().getLongitude()))
                .addFormDataPart("video[caption]", video.getCaption())
                .addFormDataPart("video[created_at]", String.valueOf(video.getCreatedAt()))
                .addFormDataPart("video[updated_at]", String.valueOf(video.getUpdatedAt()))
                .build();

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                // Handle the error
                Log.d(TAG, "video upload request failed" + request.body().toString() + "error = " + e.getMessage());
            }

            @Override
            public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                if (!response.isSuccessful()) {
                    // Handle the error

                }
                // Upload successful
                Log.d(TAG, "video upload request successful with response = " + response.body().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        return true;
    } catch (Exception ex) {
        // Handle the error
    }
    return false;
}

0 个答案:

没有答案