2013-04-16 125 views
1

我正在使用jpachube,并遇到了creatDatastream上的.POST问题。我收到POST错误400,并从COSM的调试工具以下细节:COSM JSON解析器错误.POST

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

从COSM调试工具我的XML请求体如下:对于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的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")); 
     } 
    } 

这会因.POST的COSM调试工具下面的错误(错误代码422):

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

因此,很自然,我需要得到这个请求标题。那就是通过toXMLWithWrapper在Data.java

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已经被采纳”。哎呀!

+0

我想指出,存在一个新的Java库,你可以在这里找到:https://github.com/cosm/cosm-java – errordeveloper

+0

访问被拒绝!我不是绝地(或某个东西),所以我不能看它。 – user2056097

+0

我的不好,它在私人测试版!您可以通过电子邮件发送Cosm支持以访问。 – errordeveloper

回答

0

线索是,系统看起来像它期待json,但你喂它的XML。 v2 api的默认值是json,所以你需要确保你在URL中包含了XML。G:

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

或者,也可以在请求中设置的内容类型报头,以指示该:

Content-Type: application/xml 
+0

嗨 - 我尝试了你的建议(谢谢!)。我用我的观察编辑了我原来的帖子,但是总的来说,它看起来像COSM窒息了,或者因为我在.POST(.GET工作正常)上获得了HTTP://1.1 500。 – user2056097

1

这似乎是XML可能是无效的。

开幕<environment>节点似乎被两次<environment>>

+0

lebreeze - 检查请求正文,你看到的是一个错字(如果固定在原来的文章) – user2056097

+0

检查lebreeze!在我的XML中有一个错字,我修复了它,并且我回到了422错误 - 我将把这些信息放在上面的原始文章中。 – user2056097

1

的422可能是因为你正试图POST到现有的饲料关闭。

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

Updating a feed docs

+0

嗨 - 看起来你可能是正确的,将继续调试和张贴时,我有一些洞察力和/或有用的东西。谢谢。 – user2056097