1

我正在使用 jpachube,并且在 creatDatastream 上遇到了 .POST 的问题。我收到 POST 错误 400,以及来自 COSM 调试工具的以下详细信息:

{"title":"JSON Parser Error","errors":"lexical error: invalid char in json text. <?   xmlversion=\"1.0\"encoding=\"U"}

我来自 COSM 调试工具的 XML 请求正文如下:

<?xml version="1.0" encoding="UTF-8"?>
       <eeml xmlns="http://www.eeml.org/xsd/005"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5" xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd"><environment><data id="0">
       <tag>CPU</tag>
            <current_value>0.0</current_value>
        </data>
       </environment>
   </eeml>

COSM 的 xml 请求正文的 API 文档如下:

<eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-     instance" version="0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1   http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
  <environment>
    <data id="23">
      <tag>apple</tag>
      <tag>jag</tag>
      <tag>tag</tag>
      <tag>lag</tag>
      <current_value>11</current_value>
      <max_value>211.0</max_value>
      <min_value>7.0</min_value>
    <unit type="conversionBasedUnits" symbol="symbol">label</unit>
  </data>
</environment>

我发现的唯一区别是版本号,但我已经在代码中进行了切换并得到了同样的错误。

我认为 COSM API 的 v2 已设置,因此 xml 和 JSON 可以互换,但它将所有内容都转换为 JSON。

错误来自 Pachube.java 中的此方法调用

public boolean createDatastream(int feed, String s) throws PachubeException {
        HttpRequest hr = new HttpRequest("http://api.cosm.com/v2/feeds/"
                + feed + "/datastreams/");
        hr.setMethod(HttpMethod.POST);
        hr.addHeaderItem("X-PachubeApiKey", this.API_KEY);
        hr.setBody(s);
        HttpResponse g = this.client.send(hr);

        if (g.getHeaderItem("Status").equals("HTTP/1.1 201 Created")) {
            return true;
        } else {
            throw new PachubeException(g.getHeaderItem("Status"));
        }
    }

任何输入表示赞赏。

第二天...

使用来自 bjpirt 的输入修改了 createDatastream 方法(非常感谢)。方法看起来像这样

public boolean createDatastream(int feed, String s) throws PachubeException {

        HttpRequest hr = new HttpRequest("http://api.cosm.com/v2/feeds/"
                + feed + "/datastreams.xml");
        hr.setMethod(HttpMethod.POST);
        hr.addHeaderItem("X-PachubeApiKey", this.API_KEY);
        hr.addHeaderItem("Content-Type:", "application/xml");
        hr.setBody(s);
        HttpResponse g = this.client.send(hr);

        if (g.getHeaderItem("Status").equals("HTTP/1.1 201 Created")) {
            return true;
        } else {
            Log.d("create data stream", "prob");
            throw new PachubeException(g.getHeaderItem("Status"));
        }
    }

这会在 COSM 调试工具上为 .POST 引发以下错误(错误代码 422):

<?xml version="1.0" encoding="UTF-8"?><errors><title>Unprocessable Entity</title>         <error>Stream ID has already been taken</error></errors>

所以,很自然,我需要得到这个请求的标题。这是通过 Data.java 中的 toXMLWithWrapper 完成的

public String toXMLWithWrapper() {
    String ret = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<eeml xmlns=\"http://www.eeml.org/xsd/005\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"5\" xsi:schemaLocation=\"http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd\"><environment>";
    ret = ret + ">\n\t<title>" + "cosm app" + "</title>\n\t";//inserted this line to add title
    ret = ret + this.toXML() + "</environment></eeml>";
    return ret;
}

请求正文看起来像(来自 COSM 调试工具):

<?xml version="1.0" encoding="UTF-8"?>
<eeml xmlns="http://www.eeml.org/xsd/005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5" xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd"><environment>
<title>cosm app</title>
<data id="0">
    <tag>CPU</tag>
    <current_value >0.0</current_value>
</data></environment></eeml>

这以错误代码 500 的形式返回(哎哟!)

响应正文是

<?xml version="1.0" encoding="UTF-8"?><errors><title>Oops, something's broken</title>  <error>We've been unable to complete your request due to a problem with our server</error></errors>

第三天

有人指出,xml 有问题(见下文)。我修正了错字,我又回到了 422 错误。因此,更仔细地查看响应正文,我认为数据流可能有问题。我删除了提要中的所有数据流,创建了一个新提要,我得到了一个非常棒的 HTTP:/1.1 201 - 很高兴,对吧?错了,在第一个 .POST 之后我什么也没得到。当我关闭应用程序然后重新打开时,我回到 422 错误并且相同的响应正文“流 ID 已被采用”。哎呀!

4

3 回答 3

1

看起来xml可能无效。

打开<environment>节点似乎关闭了两次<environment>>

于 2013-04-16T22:55:43.967 回答
1

422 可能是因为您正在尝试访问POST现有的提要。

要更新提要,您需要发送PUT请求。

请参阅更新提要文档

于 2013-04-18T08:12:24.663 回答
0

线索是系统看起来需要 json,但您正在向它提供 XML。v2 api 的默认值为 json,因此您需要确保在 URL 中包含 XML,例如:

https://api.cosm.com/v2/feeds/113/datastreams.json

或者,您可以在请求上设置内容类型标头以表明这一点:

Content-Type: application/xml
于 2013-04-16T07:03:43.477 回答