如何使用Multipart Volley Request。?

时间:2013-08-19 05:39:30

标签: android android-volley

我想在多部分截击请求中上传视频。只是想知道,如何使用以及如何在其中添加多部分参数。?

1 个答案:

答案 0 :(得分: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;
    }
}