I am using the snippet below to retrieve data from the server. this data can be really big. Looking for better ways to optimise this or a fix to the snippet. Thanks:
URL url = new URL("http://www.example.com" + path);
Map<String, Object> params = new LinkedHashMap<>();
params.put("param_1", value_1);
params.put("param_2",value_2);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.setConnectTimeout(3000);
conn.getOutputStream().write(postDataBytes);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String output = "";
String line = null;
while ((line = in.readLine()) != null) {
output += (line + "\n"); // this take forever with large data set
}