Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java .POST上的COSM JSON解析器错误_Java_Json_Cosm - Fatal编程技术网

Java .POST上的COSM JSON解析器错误

Java .POST上的COSM JSON解析器错误,java,json,cosm,Java,Json,Cosm,我正在使用jpachube,并且在creatDatastream上遇到了.POST问题。我从COSM的调试工具获得POST error 400和以下详细信息: {"title":"JSON Parser Error","errors":"lexical error: invalid char in json text. <? xmlversion=\"1.0\"encoding=\"U"} <?xml version="1.0" encoding="UTF-8"?> &l

我正在使用jpachube,并且在creatDatastream上遇到了.POST问题。我从COSM的调试工具获得POST error 400和以下详细信息:

{"title":"JSON Parser Error","errors":"lexical error: invalid char in json text. <?   xmlversion=\"1.0\"encoding=\"U"}
<?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>
欢迎您的任何意见

第二天

使用来自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):

已获取不可处理的实体流ID
所以,很自然,我需要在这个请求上获得一个标题。这是通过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;
}
公共字符串toXMLWithWrapper(){
字符串ret=“\n”;
ret=ret+“>\n\t”+“cosm应用程序”+“\n\t”;//插入此行以添加标题
ret=ret+this.toXML()+“”;
返回ret;
}
请求主体看起来像(来自COSM调试工具):


cosm应用程序
中央处理器
0
这返回为错误代码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>
Oops,有些东西坏了,由于服务器出现问题,我们无法完成您的请求
第三天


有人指出xml有一个问题(见下文)。我修正了输入错误,又回到了422错误。因此,更仔细地观察响应体,我认为数据流可能有问题。我删除了feed中的所有数据流,创建了一个新的feed,我得到了一个非常棒的HTTP:/1.1201-高兴,对吧?错了,第一次发完后,我什么也得不到。当我关闭应用程序,然后再打开,我回到422错误和相同的响应主体“流ID已经采取”。哎呀

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

https://api.cosm.com/v2/feeds/113/datastreams.json
或者,您可以在请求上设置一个内容类型标头,以指示:

Content-Type: application/xml

似乎xml可能无效


打开的
节点似乎关闭了两次

422可能是因为您试图
发布到现有提要

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


请参见

我想指出的是,您可以在这里找到一个新的Java库:拒绝访问!我不是绝地武士(或什么的),所以我不能看它。我的错,它在私人测试版!您可以通过电子邮件发送Cosm支持以获得访问权限。您好-我尝试了您的建议(谢谢!)。我用我的观察编辑了我的原始帖子,但总的来说,它看起来像是COSM阻塞了或是我在.POST.GET working fine.lebreeze上得到了500个什么。lebreeze-检查了请求主体,你看到的是一个拼写错误(如果在原始帖子中修复了它)检查lebreeze!我的xml中有一个输入错误,我修复了它,现在又出现了422错误-我会将此信息放在上面的原始帖子中。您好-看起来您可能是对的,当我得到一些有见地和/或有用的东西时,将继续调试和发布。谢谢
<?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>
<?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>
https://api.cosm.com/v2/feeds/113/datastreams.json
Content-Type: application/xml