我正在使用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>有关xml请求体应该是什么样子的,COSM的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的v2,API是这样设置的,所以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.1201- happy,对吧?错,在第一次.POST之后,我什么也得不到。当我关闭应用程序然后重新打开时,我返回到422错误和相同的响应体"Stream ID已被采用“。呀!
发布于 2013-04-17 06:55:44
似乎xml可能是无效的。
打开的<environment>节点似乎关闭了两次<environment>>
发布于 2013-04-18 16:12:25
422可能是因为您试图通过POST连接到现有提要。
要更新提要,您需要发送PUT请求。
请参阅Updating a feed docs
发布于 2013-04-16 15:03:43
线索是,系统看起来像是在等待json,但您向它提供的是XML。URL的默认值是json,因此您需要确保在v2中包含XML,例如:
https://api.cosm.com/v2/feeds/113/datastreams.json或者,您可以在请求上设置内容类型标头,以指示以下内容:
Content-Type: application/xmlhttps://stackoverflow.com/questions/16028996
复制相似问题