引起:com.google.gson.stream.MalformedJsonException:第1行第21列的预期EOF

时间:2013-05-09 16:22:51

标签: java rally sample

我正在尝试运行示例java程序以从集会中检索前5个缺陷。我收到以下错误:

Querying for top 5 highest priority unfixed defects...
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 21
    at com.google.gson.JsonParser.parse(JsonParser.java:65)
    at com.google.gson.JsonParser.parse(JsonParser.java:45)
    at com.rallydev.rest.response.Response.(Response.java:25)
    at com.rallydev.rest.response.QueryResponse.(QueryResponse.java:16)
    at com.rallydev.rest.RallyRestApi.query(RallyRestApi.java:179)
    at QueryExample.main(QueryExample.java:36)
Caused by: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 21
    at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1298)
    at com.google.gson.stream.JsonReader.peek(JsonReader.java:390)
    at com.google.gson.JsonParser.parse(JsonParser.java:60)
    ... 5 more

源代码如下:

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;



public class QueryExample {

    public static void main(String[] args) throws URISyntaxException, IOException {

        //Create and configure a new instance of RallyRestApi
        RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com/#/4608446320ud"), "stephen5@netapp.com", "password");
        restApi.setApplicationName("QueryExample");

        try {

            System.out.println("Querying for top 5 highest priority unfixed defects...");

            QueryRequest defects = new QueryRequest("defect");

            defects.setFetch(new Fetch("FormattedID", "Name", "State", "Priority"));
            defects.setQueryFilter(new QueryFilter("State", "<", "Fixed"));
            defects.setOrder("Priority ASC,FormattedID ASC");

            //Return up to 5, 1 per page
            defects.setPageSize(1);
            defects.setLimit(5);

            QueryResponse queryResponse = restApi.query(defects);
            if (queryResponse.wasSuccessful()) {
                System.out.println(String.format("\nTotal results: %d", queryResponse.getTotalResultCount()));
                System.out.println("Top 5:");
                for (JsonElement result : queryResponse.getResults()) {
                    JsonObject defect = result.getAsJsonObject();
                    System.out.println(String.format("\t%s - %s: Priority=%s, State=%s",
                    defect.get("FormattedID").getAsString(),
                    defect.get("Name").getAsString(),
                    defect.get("Priority").getAsString(),
                    defect.get("State").getAsString()));
                }
            } else {
                System.err.println("The following errors occurred: ");
                for (String err : queryResponse.getErrors()) {
                    System.err.println("\t" + err);
                }
            }

        } finally {
            //Release resources
            restApi.close();
        }
    }
}          

非常感谢任何有关导致此问题的帮助!

1 个答案:

答案 0 :(得分:0)

查看您的代码我认为您的网址:

URI("https://rally1.rallydev.com/#/4608446320ud")

是问题所在,它只需要:

URI("https://rally1.rallydev.com")

Rally REST URL的格式如下:

https://rally1.rallydev.com/slm/webservice/1.43/defect/12345678910.js

因此,当Java REST库试图针对从您的代码构建的URL制定查询时,它最终会看起来像这样:

https://rally1.rallydev.com/#/4608446320ud/slm/webservice/1.43/defect/12345678910.js

如果您将其插入浏览器,您将获得一个Rally 404,其中包含大量HTML和格式,而不是JSON。 “#/ 4608446320ud”字符串仅在RallyUI中使用,并在App中提供项目范围信息。该UI上下文解释了为什么您将获得Java工具包不知道如何处理的格式化HTML 404响应。