Java coding notes III

  • Sending HTTP post request

In case of sending HTTP post request to a URL with parameters such as submitting a HTML form and expecting to receive some response.

More details: Apache HTTP Client

import org.apache.http.client.*;

//import org.apache.commons.httpclient.HttpException;
//import org.apache.commons.httpclient.HttpStatus;
//import org.apache.commons.httpclient.methods.PostMethod;

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://someUrl");
List<NameValuePair> paraList = new ArrayList<NameValuePair>();
paraList.add(new BasicNameValuePair("para1", para1));
paraList.add(new BasicNameValuePair("para2", para2));

httpPost.setEntity(new UrlEncodedFormEntity(paraList, "UTF-8"));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

try {
System.out.println(httpResponse.getStatusLine());
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity!= null) {
InputStream instream = httpEntity.getContent();
}
} catch (HttpException e) {
//...
} catch (IOException e) {
//...
} finally {
httpResponse.close();
}

Another method or an old way …

import java.net.*;

try {
URL url = new URL("https://someUrl");
//open connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//set connection output | input to true
connection.setDoOutput(true);
connection.setDoInput(true);
//instead of a GET, using method="POST"
connection.setRequestMethod("POST");
//an OutputStreamWriter for output stream by getOutputStream which writes to the connection.
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
//write data
String para = URLEncoder.encode(apiKey, "UTF-8");
writer.write("para=" + para);
writer.flush();
//read from connection.getInputStream
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String readLine;
while ((readLine = reader.readLine()) != null) {
//process readLine...
}
writer.close();
reader.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
//connection is good
} else {
//HTTP error code.
}
} catch (MalformedURLException e) {
//...
} catch (IOException e) {
//...
}
  • Scaling BigDecimal

Some methods for scaling a BigDecimal number.

import java.math.*

BigDecimal amount = new BigDecimal(98.7654);

//option A: output 9876
BigDecimal amount1 = amount.scaleByPowerOfTen(2).setScale(0, RoundingMode.HALF_UP);
System.out.println(String.valueOf(amount1));

//option B: output 9876.54
BigDecimal amount2 = amount.movePointRight(2).setScale(2, RoundingMode.HALF_UP);
System.out.println(String.valueOf(amount2));

//option C: output 9876.5400
BigDecimal amount3 = amount.multiply(new BigDecimal(100)).setScale(4, RoundingMode.HALF_UP);
System.out.println(String.valueOf(amount3));

//output: 12.3
System.out.println(String.valueOf(new BigDecimal("12.3456").round(new MathContext(3, RoundingMode.HALF_UP))));

//output: 12.35
System.out.println(String.valueOf(new BigDecimal("12.3456").setScale(2, RoundingMode.HALF_UP)));