Difference between revisions of "Template:Base Code for Consuming API Using Java"
From Updox API
Line 1: | Line 1: | ||
− | private | + | private HttpResponse SendReceiveJSON(String jsonData, String uri) throws Exception { |
HttpClient httpClient = new DefaultHttpClient(); | HttpClient httpClient = new DefaultHttpClient(); | ||
+ | HttpResponse response = null; | ||
StringEntity params = new StringEntity(jsonData); | StringEntity params = new StringEntity(jsonData); | ||
try { | try { | ||
Line 6: | Line 7: | ||
request.addHeader("content-type", "application/json"); | request.addHeader("content-type", "application/json"); | ||
request.setEntity(params); | request.setEntity(params); | ||
− | + | response = httpClient.execute(request); | |
} catch (Exception ex) { | } catch (Exception ex) { | ||
// handle exception here | // handle exception here | ||
Line 12: | Line 13: | ||
httpClient.getConnectionManager().shutdown(); | 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; }