Codename One - 如何在使用multipart请求上传文件后获得响应

时间:2014-05-24 17:41:53

标签: java vimeo codenameone vimeo-api

我正在使用Codename One使用各自的API将视频上传到Vimeo。我正在使用多部分请求来实际上传文件,但需要响应才能找到上传状态。我怎样才能得到答复?

public void doFileUpload(String url, String filename) throws IOException {
    MultipartRequest req = new MultipartRequest() {
        int chr;
        StringBuffer sb = new StringBuffer();
        public String response = "";

        public void readResponse(InputStream input) throws IOException {
            response = Util.readToString(input);

            Log.p("File Response->" + response);
        }

        protected void handleErrorResponseCode(int code, String message)
        {
            Log.p("Error Response->" + message);
        }

        protected void readHeaders(Object connection) throws IOException {
            String val = getHeader(connection, "MyHeaderName");
            Log.p("Header Response->" + val);

        }

        protected void handleException(Exception err) {
            Dialog.show(
                    "Connection Err!!",
                    "Are you connected to the internet? Check your connection",
                    "Ok", null);
        }
    };
    req.setUrl(url);
    req.setPost(true);

    String mime = "application/mp4";
    req.addData("file_data", filename, mime);
    req.setFilename("file_data", filename);
    req.setReadResponseForErrors(true);
    req.addResponseCodeListener(new ActionListener() {

        public void actionPerformed(ActionEvent ev) {
            try {
                NetworkEvent event = (NetworkEvent) ev;
                Log.p("Err Rsp Code->" + event.getResponseCode());
                Log.p("ResponseCodeListener:");
                Log.p(ev.toString() );

2 个答案:

答案 0 :(得分:0)

我建议您使用接受文件URL而不是字节数组的addData方法,因为您可能会超出具有字节数组版本的设备内存(例如,对于较大的视频或低内存设备)。

您可以像任何其他连接请求一样阅读响应:

  1. 导出连接请求并覆盖:protected void readResponse(InputStream input)
  2. 使用addResponseListener将侦听器绑定到用户响应。
  3. 使用addToQueueAndWait检测上传完成后再调用getResponseData()以获取字节数组数据。

答案 1 :(得分:0)

我遇到了同样的问题!在后端,我正在使用Spring boot,将我的上载控制器更改为@Controller时被简单标记为@RestController,它工作正常,并且得到了readResponse(InputStream input)回调,其中我将响应作为JSON处理。