Try this one.. I used below code,
private int setSprint(String sprint, String key, String boardId) {
String _jiraUser = applicationProperties.get(Constants.JIRAUSER);
String _jiraPwd = applicationProperties.get(Constants.JIRAPWD);
String auth = new String(Base64.encode(_jiraUser + ":" + _jiraPwd));
String agileURL = applicationProperties.get(Constants.JIRAURL)
+ "/rest/agile/1.0/board/" + boardId + "/sprint";
int sprintId = invokeGetMethod(auth, agileURL, sprint);
// Constants.REPORT.info(sprintId);
String issueURL = applicationProperties.get(Constants.JIRAURL)
+ "/rest/agile/1.0/issue/" + key;
int issueId = getIssueId(auth, issueURL, key);
// Constants.REPORT.info(issueId);
return invokePostMethod(auth, sprintId, key, issueId);
// Constants.REPORT.info(statusCode);
}
private static int invokeGetMethod(String auth, String agileURL,
String sprint) {
int sprintId = 0;
int startAt = 0;
agileURL = agileURL + "?startAt=" + startAt;
sprintId = getSprintResult(agileURL, auth, sprint, startAt);
// sprintId = getSprintId(content, sprint, agileURL);
return sprintId;
}
private static int getSprintResult(String agileURL, String auth,
String sprint, int startAt) {
HttpGet post = new HttpGet(agileURL);
org.apache.http.impl.client.DefaultHttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient();
post.setHeader("Content-type", "application/json");
post.setHeader("Authorization", "Basic " + auth);
HttpResponse response = null;
String content = null;
try {
response = httpClient.execute(post);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
content = CharStreams.toString(new InputStreamReader(response
.getEntity().getContent(), Charsets.UTF_8));
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int SprintId = getSprintId(content, sprint, agileURL, startAt, auth);
if (SprintId != 0)
return SprintId;
return SprintId;
}
static int getSprintId(String content, String sprint, String agileURL,
int startAt, String auth) {
boolean flag = false;
int sprintId = 0;
Gson gson = new Gson(); // Class<String> data=new Class<String>();
Data values = gson.fromJson(content, Data.class);
for (Values data : values.getValues()) { // data.get
if (data.getName().equalsIgnoreCase(sprint))
// Constants.REPORT.info(sprintId=data.getId());
{
flag = true;
// Constants.REPORT.info(data);
// statusCode = response.getEntityInputStream().;
return sprintId = data.getId();
}
}
if (!flag) {
//Pagination
agileURL = agileURL.replaceAll(
"startAt=" + String.valueOf(startAt),
"startAt=" + String.valueOf(startAt += 50));
sprintId = getSprintResult(agileURL, auth, sprint, startAt);
if (sprintId != 0)
return sprintId;
}
return sprintId;
}
private static int getIssueId(String auth, String agileURL, String key) {
int issueId = 0;
try {
org.apache.http.impl.client.DefaultHttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient();
HttpGet post = new HttpGet(agileURL);
post.setHeader("Content-type", "application/json");
post.setHeader("Authorization", "Basic " + auth);
HttpResponse response = httpClient.execute(post);
String content = CharStreams.toString(new InputStreamReader(
response.getEntity().getContent(), Charsets.UTF_8));
Gson gson = new Gson(); // Class<String> data=new Class<String>();
IssueData issueValues = gson.fromJson(content, IssueData.class);
if (issueValues.getKey().equalsIgnoreCase(key))
issueId = issueValues.getId();
// Constants.REPORT.info(issueValues);
} catch (Exception e) {
Constants.ERROR.info(Level.INFO, e);
// vjErrorLog.info(Level.INFO, e);
}
return issueId;
}
private static int invokePostMethod(String auth, int sprintId, String key,
int issueId) {
int statusCode = 0;
try {
String postUrl = applicationProperties.get(Constants.JIRAURL)
+ "/rest/agile/1.0/sprint/" + sprintId + "/issue";
String str = "{\"issues\": [\"" + key + "\",\"" + issueId + "\"]}";
statusCode = invokePostMethod(auth, postUrl, str);
return statusCode;
} catch (Exception e) {
Constants.ERROR.info(Level.INFO, e);
// vjErrorLog.info(Level.INFO, e);
}
return statusCode;
}
Create a IssueData class with below attribute. and create getter/setter also overeride toString() method.
private int id;
private String self;
private String key;