Difference between revisions of "Template:Base Code for Consuming API Using Java"

From Updox API
Jump to: navigation, search
(Created page with " private void SendReceiveJSON(String json, String url) throws Exception { HttpClient httpClient = new DefaultHttpClient(); try { HttpPost request = new HttpPost...")
 
 
(5 intermediate revisions by one user not shown)
Line 1: Line 1:
private void SendReceiveJSON(String json, String url) throws Exception {  
+
    private HttpResponse SendReceiveJSON(String jsonData, String uri) throws Exception {
    HttpClient httpClient = new DefaultHttpClient();
+
        HttpClient httpClient = new DefaultHttpClient();
    try {
+
        HttpResponse response = null;
      HttpPost request = new HttpPost("{{Base url api|ping|ping}}");
+
        StringEntity params = new StringEntity(jsonData);
      StringEntity params = new StringEntity("{}");
+
        try {
      request.addHeader("content-type", "text/json");
+
            HttpPost request = new HttpPost(uri);
      request.setEntity(params);
+
            request.addHeader("content-type", "application/json");
      HttpResponse response = httpClient.execute(request);
+
            request.setEntity(params);
    } catch (Exception ex) {
+
            response = httpClient.execute(request);
      // handle exception here
+
        } catch (Exception ex) {
    } finally {
+
            // handle exception here
      httpClient.getConnectionManager().shutdown();
+
        } finally {
  }
+
            httpClient.getConnectionManager().shutdown();
 +
        }
 +
        return response;
 +
    }

Latest revision as of 18:47, 19 November 2013

   private HttpResponse SendReceiveJSON(String jsonData, String uri) throws Exception {
       HttpClient httpClient = new DefaultHttpClient();
       HttpResponse response = null;
       StringEntity params = new StringEntity(jsonData);
       try {
           HttpPost request = new HttpPost(uri);
           request.addHeader("content-type", "application/json");
           request.setEntity(params);
           response = httpClient.execute(request);
       } catch (Exception ex) {
           // handle exception here
       } finally {
           httpClient.getConnectionManager().shutdown();
       }
       return response;
   }