【问题标题】:How to upload a video along with other parameters in JSON format如何以 JSON 格式上传视频和其他参数
【发布时间】:2023-03-16 09:13:01
【问题描述】:

我想执行一个发布请求。我的 POST 数据将有几个字符串参数以及视频数据。如何做到这一点? 上传视频以及字符串、数组、NSDictionary 值等其他参数的最佳方式是什么?

【问题讨论】:

    标签: iphone json video upload ios8


    【解决方案1】:

    您通常会使用 HTTP MultiPart Mime POST 请求消息来执行此操作 - 该消息可以包含视频和您想要包含的任何参数。

    根据您的标签,我猜您想在 iOS 中执行此操作 - 如果是这样,此答案提供了一个完整示例:https://stackoverflow.com/a/24252378/334402

    如果您想在 Android 中执行此操作,则可以使用以下方法(请参阅添加的标题和描述参数):

            //Create a new Multipart HTTP request to upload the video
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(serverURL);
    
            //Create a Multipart entity and add the parts to it
            try {
                Log.d("VideoUploadTask doInBackground","Building the request for file: " + videoPath);
                FileBody filebodyVideo = new FileBody(new File(videoPath));
                StringBody title = new StringBody("Filename:" + videoPath);
                StringBody description = new StringBody("Test Video");
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("videoFile", filebodyVideo);
                reqEntity.addPart("title", title);
                reqEntity.addPart("description", description);
                httppost.setEntity(reqEntity);
            } catch (UnsupportedEncodingException e1) {
                //Log the error
                Log.d("VideoUploadTask doInBackground","UnsupportedEncodingException error when setting StringBody for title or description");
                e1.printStackTrace();
                return -1;
            }
    
            //Send the request to the server
            HttpResponse serverResponse = null;
            try {
                Log.d("VideoUploadTask doInBackground","Sending the Request");
                serverResponse = httpclient.execute( httppost );
            } catch (ClientProtocolException e) {
                //Log the error
                Log.d("VideoUploadTask doInBackground","ClientProtocolException");
                e.printStackTrace();
            } catch (IOException e) {
                //Log the error
                Log.d("VideoUploadTask doInBackground","IOException");
                e.printStackTrace();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2022-10-06
      相关资源
      最近更新 更多