Main Refactor
- Main now is called SeniorAssistant - Moved all threads methods of main in VariousThreads - Added class VariousThreads - Handled rest exception better
This commit is contained in:
@@ -41,23 +41,22 @@ public class Rest {
|
||||
public static Map<String, ?> get(String URL) {
|
||||
Map<String, ?> response = new HashMap<>();
|
||||
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
HttpGet request = new HttpGet(URL);
|
||||
|
||||
CloseableHttpResponse result;
|
||||
try {
|
||||
result = httpclient.execute(request);
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
HttpGet request = new HttpGet(URL);
|
||||
CloseableHttpResponse result = httpclient.execute(request);
|
||||
String json = EntityUtils.toString(result.getEntity());
|
||||
// do something useful with the response body
|
||||
|
||||
response = gson.fromJson(json, Map.class);
|
||||
// should be inside a finally...
|
||||
result.close();
|
||||
|
||||
httpclient.close();
|
||||
LOG.debug("GET response: " + json);
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
LOG.error("GET: " + e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -69,17 +68,16 @@ public class Rest {
|
||||
* @param contentType the content type of the request
|
||||
*/
|
||||
public static void put(String URL, String contentBody, String contentType) {
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
HttpPut request = new HttpPut(URL);
|
||||
|
||||
StringEntity params;
|
||||
try {
|
||||
params = new StringEntity(contentBody);
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
HttpPut request = new HttpPut(URL);
|
||||
StringEntity params = new StringEntity(contentBody);
|
||||
|
||||
request.addHeader("content-type", contentType);
|
||||
request.setEntity(params);
|
||||
// I don't really care about the response
|
||||
|
||||
HttpResponse result = httpclient.execute(request);
|
||||
// should be in finally...
|
||||
|
||||
httpclient.close();
|
||||
} catch (Exception e) {
|
||||
LOG.error("PUT: " + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user