0

我正在尝试从 Rally 数据库中查询某些内容。现在我只是想确保我最初可以通过。这段代码:

//create rallyrest object
RallyRestApi restApi = new RallyRestApi(new URI(hostname), username, password);
restApi.setApplicationName("QueryTest");
restApi.setWsapiVersion("v2.0");
restApi.setApplicationVersion("1.1");
System.out.println("1: So far, so good -- RallyRestApi object created");

    try {

        //create query request
        String type = "HierarchicalRequirement";
        QueryRequest qreq = new QueryRequest(type);
        System.out.println("2: Still going -- Query Request Created");

        //set fetch, filter, and project
        qreq.setFetch(new Fetch("Name","FormattedID"));
        qreq.setQueryFilter(new QueryFilter("Name", "contains", "freight"));
        qreq.setProject(projectNumber);

        System.out.println("3: Going strong -- Fetch, Filter, and Project set");

        //create response from query********Blows up
        QueryResponse resp = restApi.query(qreq);
        System.out.println("4: We made it!");

    } catch (Exception e) {
    System.out.println("Error: " + e);
    }
    finally {
        restApi.close();
    }
  }

给我这个错误:

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 22
at com.google.gson.JsonParser.parse(JsonParser.java:65)
at com.google.gson.JsonParser.parse(JsonParser.java:45)
at com.rallydev.rest.response.Response.<init>(Response.java:25)
at com.rallydev.rest.response.QueryResponse.<init>(QueryResponse.java:18)
at com.rallydev.rest.RallyRestApi.query(RallyRestApi.java:227)
at RQuery.main(RQuery.java:65)

Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 22
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:531)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:414)
at com.google.gson.JsonParser.parse(JsonParser.java:60)
... 5 more
Java Result: 1

有人可以解释为什么会这样吗?我的代码错了吗?如果我需要按照错误提示进行操作并设置 Json.lenient(true),请向我提供有关如何使用我的代码执行此操作的说明。

谢谢!

4

1 回答 1

0

你的代码对我有用。我看不出代码有什么问题。尝试不同的查询,也许有一些额外的字符。请参阅这篇文章- 它提到了一个带有尾随 NUL (\0) 字符的案例。也许您需要将 lenient 设置为 true,但我不知道该怎么做:使用 Rally QueryResponse 时无法直接访问它。

尝试不同查询的原因是有两个本地因素:您的 java 环境和您的数据。MalformedJsonException 表示指向数据的错误 json。您只获取“名称”和“FormattedID”,因此罪魁祸首很可能在名称中的某个地方。尝试不同的查询,例如,(FormattedID = US123)但选择名称中不包含“货运”的故事。确定至少一个特定的查询有效——它将进一步表明问题确实与数据有关,而不是与环境有关。

接下来,直接在WS API中尝试相同的查询(名称包含“货运”) ,这是一个可以测试查询的交互式文档。代码中查询的等效项也可以粘贴到浏览器中:

https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement?workspace=https://rally1.rallydev.com/slm/webservice/v2.0/workspace/123&query=(Name%20contains%20%22fraight%22)&start=1&pagesize=200&fetch=Name,FormattedID

确保将 123 in 替换/workspace/123为工作区的有效 OID。

查询是否返回或您在浏览器中看到相同的错误?如果查询返回,TotalResultCount 是多少?

结果总数将有助于进一步排除故障。您可以一次运行一页代码,并且知道 TotalResultCount 可以操纵页面大小、启动和限制以将代码缩小到存在罪魁祸首故事的页面(假设存在罪魁祸首故事)。这是一个例子:

qreq.setPageSize(200); qreq.setStart(2); qreq.setLimit(200);

最大页面大小为 200。默认值为 20。要使用的实际数量取决于 TotalResultCount。查询的起始索引从 1 开始。默认为 1。在此示例中,我们从第二页开始

我的环境是 Java SE 1.6 和这些罐子:

httpcore-4.2.4.jar

httpclient-4.2.5.jar

commons-logging-1.1.1.jar

commons-codec-1.6.jar

gson-2.2.4.jar

集会-休息-api-2.0.4.jar

于 2014-06-04T17:51:58.357 回答