0

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
                    }
Belvi Nosakhare
  • 3,107
  • 5
  • 32
  • 65
  • 2
    If you think that the response should fit in memory, use `StringBuilder` rather than string concatenation for reading in the response, to reduce the memory overhead. A safer approach overall is to write the response to a file, rather than into memory. You may have better luck using a more modern API, like OkHttp/Okio. See: http://stackoverflow.com/a/29012988/115145 – CommonsWare Oct 17 '15 at 10:18

1 Answers1

0

You need to decrease the size of the heap allocated when create BufferReader.

Default constructor for BufferReader allocate buffer for 8192 characters. Try use BufferedReader(Reader in, int size) with less count characters.

Yury Pashkov
  • 191
  • 1
  • 7