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

From Updox API
Jump to: navigation, search
Line 1: Line 1:
  private void SendReceiveJSON(StringEntity params, String uri) throws Exception {  
+
    private void SendReceiveJSON(String jsonData, String uri) throws Exception {
    HttpClient httpClient = new DefaultHttpClient();
+
        HttpClient httpClient = new DefaultHttpClient();
    try {
+
        StringEntity params = new StringEntity(jsonData);
      HttpPost request = new HttpPost(uri);
+
        try {
      request.addHeader("content-type", "application/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) {
+
            HttpResponse response = httpClient.execute(request);
      // handle exception here
+
        } catch (Exception ex) {
    } finally {
+
            // handle exception here
      httpClient.getConnectionManager().shutdown();
+
        } finally {
    }
+
            httpClient.getConnectionManager().shutdown();
  }
+
        }
 +
    }

Revision as of 16:12, 19 November 2013

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