List of usage examples for org.apache.http.entity StringEntity StringEntity
public StringEntity(String str) throws UnsupportedEncodingException
From source file:org.wso2.carbon.sample.pizzadelivery.client.PizzaDeliveryClient.java
public static void main(String[] args) { KeyStoreUtil.setTrustStoreParams();/*from w ww .j a va 2s .c o m*/ String url = args[0]; String username = args[1]; String password = args[2]; HttpClient httpClient = new SystemDefaultHttpClient(); try { HttpPost method = new HttpPost(url); if (httpClient != null) { String[] xmlElements = new String[] { "<mypizza:PizzaDeliveryStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaDelivery>\n" + " <mypizza:OrderNo>0023</mypizza:OrderNo>\n" + " <mypizza:PaymentType>Card</mypizza:PaymentType>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaDelivery>\n" + "</mypizza:PizzaDeliveryStream>", "<mypizza:PizzaDeliveryStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaDelivery>\n" + " <mypizza:OrderNo>0024</mypizza:OrderNo>\n" + " <mypizza:PaymentType>Card</mypizza:PaymentType>\n" + " <mypizza:Address>2CYL Morris Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaDelivery>\n" + "</mypizza:PizzaDeliveryStream>", "<mypizza:PizzaDeliveryStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaDelivery>\n" + " <mypizza:OrderNo>0025</mypizza:OrderNo>\n" + " <mypizza:PaymentType>Cash</mypizza:PaymentType>\n" + " <mypizza:Address>22RE Robinwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaDelivery>\n" + "</mypizza:PizzaDeliveryStream>", "<mypizza:PizzaDeliveryStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaDelivery>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:PaymentType>Card</mypizza:PaymentType>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaDelivery>\n" + "</mypizza:PizzaDeliveryStream>" }; try { for (String xmlElement : xmlElements) { StringEntity entity = new StringEntity(xmlElement); method.setEntity(entity); if (url.startsWith("https")) { processAuthentication(method, username, password); } httpClient.execute(method).getEntity().getContent().close(); } } catch (Exception e) { e.printStackTrace(); } Thread.sleep(500); // We need to wait some time for the message to be sent } } catch (Throwable t) { t.printStackTrace(); } }
From source file:test.TestJavaService.java
/** * Main. The cmdline arguments either do or don't contain the flag -remote: that's the only supported flag. If it is specified, * we test code on the AWS instance. If it is not, we test code running locally. * The first non-flag argument is the "verb", which (currently) should be one of the verbs set at the top of source file * org.qcert.javasrc.Main. Remaining non-flag arguments are file names. There must be at least one. The number of such * arguments and what they should contain depends on the verb. * @throws Exception/*from w ww .ja v a 2 s.co m*/ */ public static void main(String[] args) throws Exception { /* Parse command line */ List<String> files = new ArrayList<>(); String loc = "localhost", verb = null; for (String arg : args) { if (arg.equals("-remote")) loc = REMOTE_LOC; else if (arg.startsWith("-")) illegal(); else if (verb == null) verb = arg; else files.add(arg); } /* Simple consistency checks and verb-specific parsing */ if (files.size() == 0) illegal(); String file = files.remove(0); String schema = null; switch (verb) { case "parseSQL": case "serialRule2CAMP": case "sqlSchema2JSON": if (files.size() != 0) illegal(); break; case "techRule2CAMP": if (files.size() != 1) illegal(); schema = files.get(0); break; case "csv2JSON": if (files.size() < 1) illegal(); break; default: illegal(); } /* Assemble information from arguments */ String url = String.format("http://%s:9879?verb=%s", loc, verb); byte[] contents = Files.readAllBytes(Paths.get(file)); String toSend; if ("serialRule2CAMP".equals(verb)) toSend = Base64.getEncoder().encodeToString(contents); else toSend = new String(contents); if ("techRule2CAMP".equals(verb)) toSend = makeSpecialJson(toSend, schema); else if ("csv2JSON".equals(verb)) toSend = makeSpecialJson(toSend, files); HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(url); StringEntity entity = new StringEntity(toSend); entity.setContentType("text/plain"); post.setEntity(entity); HttpResponse resp = client.execute(post); int code = resp.getStatusLine().getStatusCode(); if (code == HttpStatus.SC_OK) { HttpEntity answer = resp.getEntity(); InputStream s = answer.getContent(); BufferedReader rdr = new BufferedReader(new InputStreamReader(s)); String line = rdr.readLine(); while (line != null) { System.out.println(line); line = rdr.readLine(); } rdr.close(); s.close(); } else System.out.println(resp.getStatusLine()); }
From source file:ATTHackPause.Main.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*w w w. ja v a2 s . c om*/ HttpGet httpGet = new HttpGet( "https://api-m2x.att.com/v2/devices/ecbcc13b43417c6d9e1dc7618bbb1e6c/streams/"); CloseableHttpResponse response1 = httpclient.execute(httpGet); // The underlying HTTP connection is still held by the response object // to allow the response content to be streamed directly from the network socket. // In order to ensure correct deallocation of system resources // the user MUST call CloseableHttpResponse#close() from a finally clause. // Please note that if response content is not fully consumed the underlying // connection cannot be safely re-used and will be shut down and discarded // by the connection manager. try { System.out.println(response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity1); } finally { response1.close(); } HttpPost httpPost = new HttpPost( "https://api-m2x.att.com/v2/devices/ecbcc13b43417c6d9e1dc7618bbb1e6c/streams/Distance/values"); // List <NameValuePair> nvps = new ArrayList <NameValuePair>(); // nvps.add(new BasicNameValuePair("value", "15")); // nvps.add(new BasicNameValuePair("timestamp", "2016-04-09T19:37:42+00:00")); // httpPost.setEntity(new UrlEncodedFormEntity(nvps)); String jsonString = "{\"values\":[{\"value\": \"99\",\"timestamp\": \"2016-04-09T20:43:49+00:00\"}]}"; //System.out.println("\n\njsonString: " + jsonString); // JSONArray jsonArray = new JSONArray(jsonString); // System.out.println("\n\njsonArray: " + jsonArray); StringEntity se = new StringEntity(jsonString); httpPost.setEntity(se); httpPost.setHeader("Content-type", "application/json"); httpPost.setHeader("X-M2X-KEY", "20bfa4a4f5ed88f94d772e8389e9a6d0"); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine()); HttpEntity entity2 = response2.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { response2.close(); } } finally { httpclient.close(); } }
From source file:myexamples.MyExamples.java
public static void main(String[] args) throws Exception { String tweetString = ""; RefEntity refEntity = new RefEntity(); String refEntityString = gson.toJson(refEntity); httpclient = HttpClients.createDefault(); try {/* w w w . j a va 2 s .c om*/ //getExample(); tweetString = getTweet(); StringEntity stringEntity = new StringEntity(refEntityString); HttpPost httppost = new HttpPost("http://localhost:9200/referenzer/reference"); httppost.setEntity(stringEntity); CloseableHttpResponse response1 = httpclient.execute(httppost); System.out.println("responseStatus=" + response1.getStatusLine()); PostResponseEntity pRE = gson.fromJson(EntityUtils.toString(response1.getEntity()), PostResponseEntity.class); System.out.println(pRE.toString(1)); //System.out.println("response="+EntityUtils.toString(response1.getEntity())); } finally { httpclient.close(); } System.out.println(tweetString); Gson gson = new Gson(); TweetResponse tweetResponse = gson.fromJson(tweetString, TweetResponse.class); System.out.println(tweetResponse.toString(1)); System.out.println(refEntityString); }
From source file:com.thed.zapi.cloud.sample.FetchExecuteUpdate.java
public static void main(String[] args) throws JSONException, URISyntaxException, ParseException, IOException { final String issueSearchUri = API_SEARCH_ISSUES.replace("{SERVER}", jiraBaseURL); /**/*w w w. ja v a2 s.co m*/ * Get Test Issues by JQL * */ //Json object for JQL search JSONObject jqlJsonObj = new JSONObject(); jqlJsonObj.put("jql", "project = SUP"); // Replace the value with Valid JQL jqlJsonObj.put("startAt", 0); jqlJsonObj.put("maxResults", 21); // maxResults to be returned by search jqlJsonObj.put("fieldsByKeys", false); String[] IssueIds = getIssuesByJQL(issueSearchUri, userName, password, jqlJsonObj); // System.out.println(ArrayUtils.toString(IssueIds)); /** * Add tests to Cycle * */ final String addTestsUri = API_ADD_TESTS.replace("{SERVER}", zephyrBaseUrl) + cycleId; /** Create JSON object by providing input values */ String[] IssueIds = new String[25]; JSONObject addTestsObj = new JSONObject(); addTestsObj.put("issues", IssueIds); addTestsObj.put("method", "1"); addTestsObj.put("projectId", projectId); addTestsObj.put("versionId", versionId); StringEntity addTestsJSON = null; try { addTestsJSON = new StringEntity(addTestsObj.toString()); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } addTestsToCycle(addTestsUri, client, accessKey, addTestsJSON); /** * Get Execution Id's by CycleId and Add them to Ad Hoc cycle of * UnScheduled Version * */ Map<String, String> executionIds = new HashMap<String, String>(); final String getExecutionsUri = API_GET_EXECUTIONS.replace("{SERVER}", zephyrBaseUrl) + cycleId + "?projectId=" + projectId + "&versionId=" + versionId; executionIds = getExecutionsByCycleId(getExecutionsUri, client, accessKey); /** * Bulk Update Executions with Status by Execution Id * */ JSONObject statusObj = new JSONObject(); statusObj.put("id", "1"); JSONObject executeTestsObj = new JSONObject(); executeTestsObj.put("status", statusObj); executeTestsObj.put("cycleId", cycleId); executeTestsObj.put("projectId", projectId); executeTestsObj.put("versionId", versionId); executeTestsObj.put("comment", "Executed by ZAPI Cloud"); for (String key : executionIds.keySet()) { final String updateExecutionUri = API_UPDATE_EXECUTION.replace("{SERVER}", zephyrBaseUrl) + key; // System.out.println(updateExecutionUri); // System.out.println(executionIds.get(key)); executeTestsObj.put("issueId", executionIds.get(key)); // System.out.println(executeTestsObj.toString()); StringEntity executeTestsJSON = null; try { executeTestsJSON = new StringEntity(executeTestsObj.toString()); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } updateExecutions(updateExecutionUri, client, accessKey, executeTestsJSON); } }
From source file:com.newproject.ApacheHttp.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); String jsonFilePath = "/Users/vikasmohandoss/Documents/Cloud/test.txt"; String url = "http://www.sentiment140.com/api/bulkClassifyJson&appid=vm2446@columbia.edu"; JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = new JSONObject(); URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); try {/*from w ww . j a va 2 s .c om*/ FileReader fileReader = new FileReader(jsonFilePath); jsonObject = (JSONObject) jsonParser.parse(fileReader); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } System.out.println(jsonObject.toString()); /*try { /*HttpGet httpGet = new HttpGet("http://httpbin.org/get"); CloseableHttpResponse response1 = httpclient.execute(httpGet); // The underlying HTTP connection is still held by the response object // to allow the response content to be streamed directly from the network socket. // In order to ensure correct deallocation of system resources // the user MUST call CloseableHttpResponse#close() from a finally clause. // Please note that if response content is not fully consumed the underlying // connection cannot be safely re-used and will be shut down and discarded // by the connection manager. try { System.out.println(response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity1); } finally { response1.close(); } HttpPost httpPost = new HttpPost("http://httpbin.org/post"); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("username", "vip")); nvps.add(new BasicNameValuePair("password", "secret")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine()); HttpEntity entity2 = response2.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { response2.close(); } } finally { httpclient.close(); }*/ try { HttpPost request = new HttpPost("http://www.sentiment140.com/api/bulkClassifyJson"); StringEntity params = new StringEntity(jsonObject.toString()); request.addHeader("content-type", "application/json"); request.setEntity(params); HttpResponse response = httpclient.execute(request); System.out.println(response.toString()); String result = EntityUtils.toString(response.getEntity()); System.out.println(result); try { File file = new File("/Users/vikasmohandoss/Documents/Cloud/sentiment.txt"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(result); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } // handle response here... } catch (Exception ex) { // handle exception here } finally { httpclient.close(); } }
From source file:org.wso2.carbon.sample.atmstats.ATMTransactionStatsClient.java
public static void main(String[] args) throws XMLStreamException { System.out.println(xmlMsgs.get(1)); System.out.println(xmlMsgs.get(2)); System.out.println(xmlMsgs.get(3)); KeyStoreUtil.setTrustStoreParams();// w w w. j a v a2 s.com String url = args[0]; String username = args[1]; String password = args[2]; HttpClient httpClient = new SystemDefaultHttpClient(); try { HttpPost method = new HttpPost(url); for (String xmlElement : xmlMsgs) { StringEntity entity = new StringEntity(xmlElement); method.setEntity(entity); if (url.startsWith("https")) { processAuthentication(method, username, password); } httpClient.execute(method).getEntity().getContent().close(); } Thread.sleep(500); // We need to wait some time for the message to be sent } catch (Throwable t) { t.printStackTrace(); } }
From source file:com.thed.zapi.cloud.sample.CreateTestWithTestSteps.java
public static void main(String[] args) throws JSONException, URISyntaxException, ParseException, IOException { final String createTestUri = API_CREATE_TEST.replace("{SERVER}", jiraBaseURL); final String createTestStepUri = API_CREATE_TEST_STEP.replace("{SERVER}", zephyrBaseUrl); /**//from w ww .jav a2 s . c o m * Create Test Parameters, declare Create Test Issue fields Declare more * field objects if required */ JSONObject projectObj = new JSONObject(); projectObj.put("id", projectId); // Project ID where the Test to be // Created JSONObject issueTypeObj = new JSONObject(); issueTypeObj.put("id", issueTypeId); // IssueType ID which is Test isse // type JSONObject assigneeObj = new JSONObject(); assigneeObj.put("name", userName); // Username of the assignee JSONObject reporterObj = new JSONObject(); reporterObj.put("name", userName); // Username of the Reporter String testSummary = "Sample Test case With Steps created through ZAPI Cloud"; // Test // Case // Summary/Name /** * Create JSON payload to POST Add more field objects if required * * ***DONOT EDIT BELOW *** */ JSONObject fieldsObj = new JSONObject(); fieldsObj.put("project", projectObj); fieldsObj.put("summary", testSummary); fieldsObj.put("issuetype", issueTypeObj); fieldsObj.put("assignee", assigneeObj); fieldsObj.put("reporter", reporterObj); JSONObject createTestObj = new JSONObject(); createTestObj.put("fields", fieldsObj); System.out.println(createTestObj.toString()); byte[] bytesEncoded = Base64.encodeBase64((userName + ":" + password).getBytes()); String authorizationHeader = "Basic " + new String(bytesEncoded); Header header = new BasicHeader(HttpHeaders.AUTHORIZATION, authorizationHeader); StringEntity createTestJSON = null; try { createTestJSON = new StringEntity(createTestObj.toString()); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } HttpResponse response = null; HttpClient restClient = new DefaultHttpClient(); try { // System.out.println(issueSearchURL); HttpPost createTestReq = new HttpPost(createTestUri); createTestReq.addHeader(header); createTestReq.addHeader("Content-Type", "application/json"); createTestReq.setEntity(createTestJSON); response = restClient.execute(createTestReq); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String testId = null; int statusCode = response.getStatusLine().getStatusCode(); // System.out.println(statusCode); HttpEntity entity1 = response.getEntity(); if (statusCode >= 200 && statusCode < 300) { String string1 = null; try { string1 = EntityUtils.toString(entity1); System.out.println(string1); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } JSONObject createTestResp = new JSONObject(string1); testId = createTestResp.getString("id"); System.out.println("testId :" + testId); } else { try { String string = null; string = EntityUtils.toString(entity1); JSONObject executionResponseObj = new JSONObject(string); System.out.println(executionResponseObj.toString()); throw new ClientProtocolException("Unexpected response status: " + statusCode); } catch (ClientProtocolException e) { e.printStackTrace(); } } /** Create test Steps ***/ /** * Create Steps Replace the step,data,result values as required */ JSONObject testStepJsonObj = new JSONObject(); testStepJsonObj.put("step", "Sample Test Step"); testStepJsonObj.put("data", "Sample Test Data"); testStepJsonObj.put("result", "Sample Expected Result"); /** DONOT EDIT FROM HERE ***/ StringEntity createTestStepJSON = null; try { createTestStepJSON = new StringEntity(testStepJsonObj.toString()); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } String finalURL = createTestStepUri + testId + "?projectId=" + projectId; URI uri = new URI(finalURL); int expirationInSec = 360; JwtGenerator jwtGenerator = client.getJwtGenerator(); String jwt = jwtGenerator.generateJWT("POST", uri, expirationInSec); System.out.println(uri.toString()); System.out.println(jwt); HttpResponse responseTestStep = null; HttpPost addTestStepReq = new HttpPost(uri); addTestStepReq.addHeader("Content-Type", "application/json"); addTestStepReq.addHeader("Authorization", jwt); addTestStepReq.addHeader("zapiAccessKey", accessKey); addTestStepReq.setEntity(createTestStepJSON); try { responseTestStep = restClient.execute(addTestStepReq); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } int testStepStatusCode = responseTestStep.getStatusLine().getStatusCode(); System.out.println(testStepStatusCode); System.out.println(response.toString()); if (statusCode >= 200 && statusCode < 300) { HttpEntity entity = responseTestStep.getEntity(); String string = null; String stepId = null; try { string = EntityUtils.toString(entity); JSONObject testStepObj = new JSONObject(string); stepId = testStepObj.getString("id"); System.out.println("stepId :" + stepId); System.out.println(testStepObj.toString()); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { try { throw new ClientProtocolException("Unexpected response status: " + statusCode); } catch (ClientProtocolException e) { e.printStackTrace(); } } }
From source file:org.wso2.carbon.sample.pizzadelivery.client.PizzaOrderClient.java
public static void main(String[] args) { KeyStoreUtil.setTrustStoreParams();/* ww w . ja v a 2 s.c om*/ String url = args[0]; boolean batchedElements = Boolean.valueOf(args[1]); HttpClient httpClient = new SystemDefaultHttpClient(); try { HttpPost method = new HttpPost(url); if (httpClient != null) { String[] xmlElements = new String[] { "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0023</mypizza:OrderNo>\n" + " <mypizza:Type>PEPPERONI</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>2</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0024</mypizza:OrderNo>\n" + " <mypizza:Type>CHEESE</mypizza:Type>\n" + " <mypizza:Size>M</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>Henry Clock</mypizza:Contact>\n" + " <mypizza:Address>2CYL Morris Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0025</mypizza:OrderNo>\n" + " <mypizza:Type>SEAFOOD</mypizza:Type>\n" + " <mypizza:Size>S</mypizza:Size>\n" + " <mypizza:Quantity>4</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>22RE Robinwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:Type>CHICKEN</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Contact>Alis Miranda</mypizza:Contact>\n" + " <mypizza:Address>779 Burl Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:Type>VEGGIE</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>" }; String[] batchedXmlElements = new String[] { "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0023</mypizza:OrderNo>\n" + " <mypizza:Type>PEPPERONI</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>2</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0024</mypizza:OrderNo>\n" + " <mypizza:Type>CHEESE</mypizza:Type>\n" + " <mypizza:Size>M</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>Henry Clock</mypizza:Contact>\n" + " <mypizza:Address>2CYL Morris Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0025</mypizza:OrderNo>\n" + " <mypizza:Type>SEAFOOD</mypizza:Type>\n" + " <mypizza:Size>S</mypizza:Size>\n" + " <mypizza:Quantity>4</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>22RE Robinwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:Type>CHICKEN</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>Alis Miranda</mypizza:Contact>\n" + " <mypizza:Address>779 Burl Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:Type>VEGGIE</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>" }; int i = 0; if (batchedElements) { for (String xmlElement : batchedXmlElements) { StringEntity entity = new StringEntity(xmlElement); method.setEntity(entity); httpClient.execute(method).getEntity().getContent().close(); System.out.println("Sent event no :" + i++); } } else { for (String xmlElement : xmlElements) { StringEntity entity = new StringEntity(xmlElement); method.setEntity(entity); httpClient.execute(method).getEntity().getContent().close(); System.out.println("Sent event no :" + i++); } } Thread.sleep(500); // We need to wait some time for the message to be sent } } catch (Throwable t) { t.printStackTrace(); } }
From source file:Main.java
public static String makeRequest1(String url, String json) { Log.v(TAG, "URL-->" + url); Log.v(TAG, "input-->" + json); try {/* w ww . j av a2 s . co m*/ HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(json)); HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost); // receive response as inputStream InputStream inputStream = httpResponse.getEntity().getContent(); // convert inputstream to string if (inputStream != null) { String result = convertInputStreamToString(inputStream); Log.v(TAG, "output-->" + result); return result; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; }