这是我用来查询“订阅”然后返回我想要访问的工作区的引用的代码:
public static String query_for_workspace(String ws, RallyRestApi r) throws IOException{
String Wspace_ref=null;
//Read Subscription
QueryRequest subscriptionRequest = new QueryRequest("Subscriptions");
subscriptionRequest.setFetch(new Fetch("Name", "Workspaces"));
QueryResponse subscriptionQueryResponse = r.query(subscriptionRequest);
String subName = subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Name").toString();
System.out.println("Read Subscription: " + subName);
// Grab Workspaces Collection
JsonArray myWorkspaces = subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Workspaces").getAsJsonArray();
// iterates through all workspaces and fetch 'ref' (string attribute) of the useful workspace
for (int i=0; i<myWorkspaces.size(); i++) {
JsonObject workspaceObject = myWorkspaces.get(i).getAsJsonObject();
String workspaceRef = workspaceObject.get("_ref").getAsString();
GetRequest workspaceRequest = new GetRequest(workspaceRef);
workspaceRequest.setFetch(new Fetch("Name"));
GetResponse workspaceResponse = r.get(workspaceRequest);
JsonObject workspaceObj = workspaceResponse.getObject();
String workspaceName = workspaceObj.get("Name").getAsString();
if(workspaceName.equals(ws)){
System.out.printf("Workspace found ==> %s\n", workspaceName);
Wspace_ref=workspaceRef;
}
}
return Wspace_ref;
}
错误来自这一行:
JsonArray myWorkspaces=subscriptionQueryResponse.getResults().get(0).getAsJsonObject().get("Workspaces").getAsJsonArray();
此代码使用 1.x API。如何访问 2.x 中的工作区列表?
谢谢!