0

I'm trying to query something from the Rally database. Right now I'm just trying to make sure I can get through initially. This code:

//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();
    }
  }

gives me this error:

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

Could someone please explain why this is happening? Is my code wrong? If I need to do as the error suggests and set Json.lenient(true), please give me instructions on how to do that with my code.

Thank you!

JulieF
  • 27
  • 5

1 Answers1

0

Your code works for me. I don't see anything wrong with the code. Try different query, maybe there are some extra characters. See this post - it mentioned a case with trailing NUL (\0) characters. Perhaps you need to set lenient to true, but I don't know how to do it: there is no direct access to it when working with Rally QueryResponse.

The reason for trying a different query is that there are two local factors: your java environment and your data. MalformedJsonException indicates bad json which points to data. You are fetching only "Name" and "FormattedID", so chances are the culprit is somewhere in the name. Try a different query, e.g. (FormattedID = US123) but choose the story that does not contain "freight" in the name. Establish that at least one particular query works - it will indicate further that the issue is indeed related to data, and not the environment.

Next, try the same query (Name contains "freight") directly in WS API, which is an interactive document where queries can be tested. An equivalent of the query in your code can also be pasted in the browser:

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

Make sure to replace 123 in /workspace/123 with the valid OID of your workspace.

Does the query return or you see the same error in the browser? If the query returns, what is the TotalResultCount?

The total result count will help to troubleshoot further. You may run your code one page at a time, and knowing the TotalResultCount it is possible to manipulate pagesize, start and limit to narrow down your code to the page where the culprit story exists (assuming that there is a culprit story). Here is an example:

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

Maximum pagesize is 200. Default is 20. The actual number to use depends on TotalResultCount. The start index for queries begins at 1. The default is 1. In this example we start with second page

My environment is Java SE 1.6 and these jars:

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

rally-rest-api-2.0.4.jar

Community
  • 1
  • 1
nickm
  • 5,936
  • 1
  • 13
  • 14
  • Thanks for responding. I have all the jars in place. I'm having trouble setting lenient to true. Could you show me an example of how to do it with my code? Although I've looked at many examples of JsonReader I'm not sure how to use it with my QueryResponse (the other example used a StringReader, not sure if/how I can do that...?). Thanks. – JulieF Jun 04 '14 at 18:35
  • Have you tried to change the query to see if different subset of data returned successfully? – nickm Jun 05 '14 at 00:11
  • Yes, I've tried changing everything: the type, the Fetch, the QueryFilter, commenting out all the non-essential settings (like ApplicationVersion, etc.). – JulieF Jun 05 '14 at 12:54
  • I found it. It was a slightly wrong url. Now I'm getting correct JSON. Thanks for your help! – JulieF Jun 05 '14 at 13:57