I have a problem with the following code. Firstly, I am new to Java with experience working with several other languages.
When I run the following on Android, these are errors on "in.close ();", jump to the catch block and run Return "; The fact is that the function does not return an empty string, it successfully returns the correct json data, although the debugger says that return json does not work, but that it works return "";
public void fetch() {
String json = getJSON();
};
private String getJSON() {
try {
URL url = new URL("http://imgur.com/gallery.json");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String json = in.readLine();
in.close();
return json;
}
catch(Exception e) {
Log.e("getJSON", e.getMessage());
return "";
}
}
I copy the same code to the Java console so that I can output an error message and see it in the debugger with breakpoints, and the code runs without errors.
try {
URL url = new URL("http://imgur.com/gallery.json");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String json = in.readLine();
in.close();
System.out.println(json);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
? Android Java ? , . ? Android?