如何使用Multipart Volley请求?

3

我想在一个multipart volley请求中上传视频。只是想知道如何使用和如何添加multipart参数。

1个回答

0
你可以试试这个,它是一个完整的工作代码,通过 multapart 发送视频或大文件到服务器。
public static Boolean SendPostToServer(FBPost postData, Context context, String videoPath) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(context.getString(R.string.url_service_post));
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        if(!videoPath.isEmpty()){

            FileBody filebodyVideo = new FileBody(new File(videoPath));
            reqEntity.addPart("uploaded", filebodyVideo);
        }
        reqEntity.addPart("userId", new StringBody(postData.userId));
        reqEntity.addPart("postText", new StringBody(postData.postText));
        if(postData.postId != null && postData.postId.length() > 0) {
            reqEntity.addPart("postId", new StringBody(postData.postId));
        }
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }

        Log.e("Response: ", s.toString());
        return true;

    } catch (Exception e) {
        Log.e(e.getClass().getName(), e.getMessage());
        return false;
    }
}

我会尝试这个。但是你确定它不会在大文件(例如:20MB到50MB)中出现内存错误吗? - Nitin Bathija
不,它会将您的数据分块发送,并且不会出现内存错误。 - Muhammad Aamir Ali
你的项目中是否已经包含了httpmime.jar文件? - Muhammad Aamir Ali
6
不使用Volley API。 - jasonhudgins
3
这个答案与排球无关! - erol yeniaras
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接