Dhrumin, I find using the Apache Http client easier than HttpURLConnection directly. The following code snippet works for me
|
|
| HttpClient client = new DefaultHttpClient(); |
HttpPost request = new HttpPost("http://" + server + "/rest/workflows/" + workflow.getUuid() + "/jobs");
String encodedUserNamePassword = Base64.encodeToString((userName + ":" + password).getBytes(), Base64.NO_WRAP);
request.addHeader("Authorization", "Basic " + encodedUserNamePassword);
request.addHeader("Content-type", "application/xml");
try {
request.setEntity(new StringEntity(new WorkflowInput(workflow.getUserInputs()).toXML()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpResponse httpResponse;
InputStream inputStream = null;
try {
httpResponse = client.execute(request);
int responseCode = httpResponse.getStatusLine().getStatusCode();
String message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
inputStream = entity.getContent();
String readFromStream = StreamUtils.readAll(inputStream);
return String.valueOf(responseCode);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
This is the toXML method
public String toXML() {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<workflowInput>");
sb.append("<userInputValues>");
for (UserInput userInput : userInputAndValues) {
sb.append("<userInputEntry ");
sb.append("key=\"").append(userInput.getName()).append("\"").append(" ");
if ("Query".equals(userInput.getType()) || "Enum".equals(userInput.getType())) {
sb.append("value=\"").append(((EditText)userInput.getView()).getText()).append("\"");
} else if ("Boolean".equals(userInput.getType())) {
sb.append("value=\"").append(((CheckBox) userInput.getView()).isChecked() == true ? "true" : "false").append("\"");
} else {
sb.append("value=\"").append(((EditText)userInput.getView()).getText()).append("\"");
}
sb.append("/>");
}
sb.append("</userInputValues>");
if (executionDateTime != null && !executionDateTime.isEmpty()) {
sb.append("<executionDateAndTime>").append(executionDateTime).append("</executionDateAndTime>");
}
if (comment != null && !comment.isEmpty()) {
sb.append("<comments>").append(comment).append("</comments>");
}
sb.append("</workflowInput>");
return sb.toString();
}