Http响应:无法将JSON转换为流

时间:2020-04-21 17:21:25

标签: java json delphi http-post tstream

我在JAVA中有一个API,可以将zip文件上传到Delphi中的服务器,并且我正在这样做,如下所示:

        DSRESTConnection conn = new DSRESTConnection();
        conn.setHost("example.com");
        conn.setPort(8080);
        TServerMethods1 proxy = new TServerMethods1(conn);
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(baos);
            ZipOutputStream zos = new ZipOutputStream(bos);
            zos.putNextEntry(new ZipEntry("test.json"));
            byte[] bytes = inputJson.getBytes();
            zos.write(bytes, 0, bytes.length);
            zos.close();
            bos.close();
            baos.close();
            TStream outZip = new TStream(baos.toByteArray());

            zipResponse = proxy.UserZipUpLoad("username", "password", 5, outZip, outZip.asByteArray().length);
        } catch (DBXException | IOException e) {
            e.printStackTrace();
        }

这是API:

    public UserZipUpLoadReturns UserZipUpLoad(String user, String pwd, int ZipType, TStream strmUpLoad, long iSize) throws DBXException {
  DSRESTCommand cmd = getConnection().CreateCommand();
  cmd.setRequestType(DSHTTPRequestType.POST);
  cmd.setText("TServerMethods1.UserZipUpLoad");
  cmd.prepare(get_TServerMethods1_UserZipUpLoad_Metadata());
  cmd.getParameter(0).getValue().SetAsString(user);
  cmd.getParameter(1).getValue().SetAsString(pwd);
  cmd.getParameter(2).getValue().SetAsInt32(ZipType);
  cmd.getParameter(3).getValue().SetAsStream(strmUpLoad);
  cmd.getParameter(4).getValue().SetAsInt64(iSize);
  getConnection().execute(cmd);
  UserZipUpLoadReturns ret = new UserZipUpLoadReturns();
  ret.ReturnCode = cmd.getParameter(5).getValue().GetAsInt32();
  ret.ReturnString = cmd.getParameter(6).getValue().GetAsString();
  ret.returnValue = cmd.getParameter(7).getValue().GetAsInt32();
  return ret;
}

要创建请求的正文,请在API中创建参数_parameter,这些参数不能出现在URL中,例如byteArray或blob:

boolean CanAddParamsToUrl = true;
        _parameters = new TJSONArray();
        for (DSRESTParameter parameter : ParametersToSend)
            if (CanAddParamsToUrl && isURLParameter(parameter))
                URL += encodeURIComponent(parameter) + '/';
            else // add the json representation in the body
            {
                CanAddParamsToUrl = false;
                parameter.getValue().appendTo(_parameters);
            }

并使用_parameters构建主体:

TJSONObject body = new TJSONObject();
                body.addPairs("_parameters", _parameters);
                p.setEntity(new StringEntity(body.toString(), "utf-8"));

我无权访问服务器端,也不知道在那里发生了什么。当我发送JSON对象或任何其他字符串时,服务器返回ok,但是一旦我将JSON压缩并发送后,服务器就会返回错误500,提示“无法将输入JSON转换为流”。我认为它所引用的JSON是正文而不是文件中的JSON字符串。

从代码的最后一部分,我可以看到为什么字符串可以工作,但是我不知道如何使用当前代码按要求发送zip文件。 反正有使用此代码吗?还是应该更改它?如果是这样,怎么办? 有人知道这是否是错误吗?

0 个答案:

没有答案