- Give your variables meaningful names.
- Declare your variables in the smallest possible scope.
- Don't assign dummy values (like
null
or""
) to variables. - Avoid side-effects in tests.
- Use automatic resource management to cleanly ensure that streams are closed.
- Determine and use the correct character encoding when converting bytes to characters.
Here is an example of your code that applies these points:
try (InputStream is = connection.getInputStream()) {
BufferedReader lines = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while (true) {
String line = lines.readLine();
if (line == null)
break;
System.out.println("Server Response " + line);
...
}
}