Open tongshen9095 opened 4 years ago
// can close the httpclient to release resource
httpclient.close()
// step1: Create a HttpClient object
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// step2: Create a HTTP request: specify method and URL
HttpGet httpget = new HttpGet("http://httpbin.org/");
System.out.println("Executing request " + httpget.getRequestLine());
/*
* Create a custom response handler
* do not need to manually consume the entity and close the response
* inheritance ResponseHandler<string>, parent class
*/
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(
final HttpResponse response) throws ClientProtocolException, IOException {
// return the response body
int status = response.getStatusLine().getStatusCode();
// status code between 200 and 300 indicates the request successed
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
/*
* step3: get response body directly via a custom response handler
* http.execute(httpget, responseHandler) call consume and close in the parent class
*/
String responseBody = httpclient.execute(httpget, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
} finally {
httpclient.close();
}
int count = isHere ? getHereCount(index) : getAwayCount(index);
is a shorthand of
if (isHere) {
count = getHereCount(index);
} else {
count = getAwayCount(index);
}
if doSomething()
and doSomethingElse()
are void
methods, we cannot use the shorthand
public JSONArray search(double lat, double lon, String keyword) {
// corner case: user does not provide keyword, then use the default
if (keyword == null) {
keyword = DEFAULT_KEYWORD;
}
try {
// encode the keyword
keyword = URLEncoder.encode(keyword, "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// step1: : Create a HttpClient object
CloseableHttpClient httpclient = HttpClients.createDefault();
// step2: Create a HTTP request: specify method and URL
String url = String.format(URL_TEMPLATE, keyword, lat, lon);
HttpGet httpget = new HttpGet(url);
/*
* Create a custom response handler
* return JSONArray
*/
ResponseHandler<JSONArray> responseHandler = new ResponseHandler<JSONArray>() {
@Override
public JSONArray handleResponse(final HttpResponse response) throws IOException {
int status = response.getStatusLine().getStatusCode();
if (status != 200) {
return new JSONArray();
}
HttpEntity entity = response.getEntity();
if (entity == null) {
return new JSONArray();
}
String responseBody = EntityUtils.toString(entity);
return new JSONArray(responseBody);
}
};
try {
// step3: get response body directly via a custom response handler
JSONArray array = httpclient.execute(httpget, responseHandler);
return array;
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return new JSONArray();
}
JSON is a format for string, not a JSONArray, so we need to convert string to JSONArray the response contains multiple JSONObjets
Example 1