This piece of code is creating memory leak issues cause of BufferedReader and InputStreamReader which I think might be happening cause of some exceptions. How should I change it?
try{
URL url = new URL(sMyUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
jsonString += str;
}
in.close();
}catch(Exception e){
}
It would be safer to close your stream using a
try..finallyblock. You might also use aStringBuilderas it is designed for concatenating strings. You should also avoid catchingExceptionand doing nothing with it. Also, your code is concatenating lines without any line-breaks. This may well not be what you want, in which caseappend("\n")when you read each line in.Here’s a version with those modifications: