gpt4 book ai didi

json - 使用 JAVA API 更新 Rally Team 成员(member)资格

转载 作者:行者123 更新时间:2023-12-01 13:06:52 25 4
gpt4 key购买 nike

我的 Java API 将来自另一个应用程序的团队成员与 Rally 进行比较。比较结果在 Rally 中更新。它采用项目名称和资源名称的引用。

代码抛出“java.lang.IndexOutOfBoundsException:索引:0,大小:0”错误。我无法发现错误。有人可以帮忙吗?以下是代码和输出

    package teammembership;

import com.google.gson.JsonArray;
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.request.UpdateRequest;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.*;
import java.sql.*;
import javax.xml.parsers.DocumentBuilder;
import org.apache.soap.util.xml.*;
import org.w3c.dom.*;
//import org.json.*;
//import static projectteammembers.JsonUtil.getJsonValue;

public class TeamMembership {

public static Connection makeConnection(String propertiesFile) throws SQLException, Exception {

Connection conn = null;

DocumentBuilder docBuilder = XMLParserUtils.getXMLDocBuilder();
Document doc = docBuilder.parse(new File(propertiesFile));

// Retrieve database parameters
Element database = (Element) doc.getElementsByTagName("database").item(0);
String url = database.getAttribute("url");
String serviceId = database.getAttribute("serviceId");
String username = database.getAttribute("username");
String password = database.getAttribute("password");
String host = url.substring(url.indexOf("//"), url.indexOf(";"));
String connectString = "jdbc:oracle:thin:@" + host + "/" + serviceId;

// Load JDBC Driver
String driverClass = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverClass);

try {
conn = DriverManager.getConnection(connectString, username, password);

} catch (SQLException ex) {
throw new SQLException(ex);
} catch (Exception ex) {
throw new Exception(ex);
}

return conn;

}


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

String host = "https://rally1.rallydev.com";
String username = "username@abc.com";
String password = "password";
//String userRef = "";
String applicationName = "update team membership";
//int queryLimit = 4000;
Connection conn = null;
String propertiesFile = "";
propertiesFile = "c:/app/c/properties_prod.xml";
String projid = "";
String resid = "";
//String returnValue = "";
String selectString = "";

RallyRestApi restApi = new RallyRestApi(
new URI(host),
username,
password);
restApi.setApplicationName(applicationName);
System.out.println(restApi.getWsapiVersion());

try {

conn = makeConnection(propertiesFile);


// Select compared records of Team member present in table1 not in table2
selectString += "select Prj_name ";
selectString += ",res_name";
selectString += " from CUST_table1_v c ";
selectString += " WHERE NOT EXISTS( select 1 from CUST_table2_v r";
selectString += " where c.prj_name = r.Prj_name and c.res_name = r.res_name)";

// Create select statement

Statement st = (Statement) conn.createStatement();

// Execute select statement

ResultSet rs = st.executeQuery(selectString);

while (rs.next()) {

projid = rs.getString("Prj_name");
resid = "(" + rs.getString("res_name") + ")";


System.out.println(projid);
System.out.println(resid);

QueryRequest projectRequest = new QueryRequest("Project");
projectRequest.setFetch(new Fetch("Name"));
projectRequest.setQueryFilter(new QueryFilter("Name", "=", projid));
QueryResponse projectQueryResponse = restApi.query(projectRequest);
JsonObject projectObj = projectQueryResponse.getResults().get(0).getAsJsonObject();

QueryRequest userRequest = new QueryRequest("User");
userRequest.setFetch(new Fetch("UserPermissions", "TeamMemberships"));
userRequest.setQueryFilter(new QueryFilter("DisplayName", "contains", resid));
QueryResponse userQueryResponse = restApi.query(userRequest);
System.out.println(userQueryResponse);
JsonObject userObject = userQueryResponse.getResults().get(0).getAsJsonObject();
//JsonObject projectObj = new JsonObject(projid);
String userRef = userObject.get("_ref").toString();
System.out.println("Found User with Ref: " + userRef);

JsonArray existTeamMemberships = (JsonArray) userQueryResponse.getResults().get(0).getAsJsonObject().get("TeamMemberships");

// add or remove projects for user
existTeamMemberships.add(projectObj);

// Setup update fields/values for Team Membership
JsonObject updateUserTeamMembershipObj = new JsonObject();
updateUserTeamMembershipObj.add("TeamMemberships", existTeamMemberships);

UpdateRequest updateTeamMembershipsRequest = new UpdateRequest(userRef, updateUserTeamMembershipObj);
UpdateResponse updateTeamMembershipResponse = restApi.update(updateTeamMembershipsRequest);


}



} catch (Exception e) {
e.printStackTrace();

} finally {
restApi.close();
conn.close();
}

}

}

以下是错误输出

    v2.0
DT-E2E Automation
(tmanjunath)

com.rallydev.rest.response.QueryResponse@193d23b
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.google.gson.JsonArray.get(JsonArray.java:92)
at teammembership.TeamMembership.main(TeamMembership.java:125)
BUILD SUCCESSFUL (total time: 11 seconds)

最佳答案

您有一个列表(确切地说是一个 ArrayList),其中不包含任何内容(没有单个对象),并且您尝试访问第一个对象(该对象不存在)。这就是错误告诉你的。您尝试访问索引 0(列表中的第一个位置),但其中没有元素(因此大小为 0)。它发生在第 125 行左右。由于您在问题中的格式似乎不正确,我只能猜测您问题中的哪一行是第 125 行(顺便说一句,我不想​​阅读 125 行代码)。所以我认为异常发生在这里:

JsonObject userObject = userQueryResponse.getResults().get(0).getAsJsonObject();

尝试追踪它。确保从 userQueryResponse.getResults() 返回的列表包含以下内容:

list = userQueryResponse.getResults();
System.out.println(list.size());

如果没有,那就是你的问题了。如果您无法解决,请提出有关此问题的具体问题,而无需发布 150 行代码。

关于json - 使用 JAVA API 更新 Rally Team 成员(member)资格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23196403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com