Java executes a catch block without errors and somehow still starts a try block

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() {

    // Get the JSON of the image gallery
    String json = getJSON();

    // json somehow has the actual correct json data and not an empty string!

};

private String getJSON() {

    try {
        // Create a URL for the desired page
        URL url = new URL("http://imgur.com/gallery.json");

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
        String json = in.readLine();
        in.close();
        return json; // <-- debugger doesn't break here!
    }
    catch(Exception e) {
        Log.e("getJSON", e.getMessage());
        return ""; // <-- debugger breaks here! Cat Log shows nothing!
    }
}

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 {
        // Create a URL for the desired page
        URL url = new URL("http://imgur.com/gallery.json");

        // Read all the text returned by the server
        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?

+3
4

<uses-permission android:name="android.permission.INTERNET" />

androidManifext.xml.

. Java , Android.

+2

"System.out.println" Android. "Logcat" Eclipse. , , . , "System.out.println(json);" "Log.e("MyApp", "Here is my Json: '"+json+"'");", json logcat...;)

, , logcat: "myException.printStackTrace();"

0

A Reader , InputStream. InputStreamReader .

:

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));

JSON UTF-8, . Android - UTF-8 , , Cp1252. , - .

0

. JSON, IOException close.

Edit

, finally . .

: ///// " , "

. ( , Run, , , . .) , , , , . , .

(Suspend On Caught Exceptions), , - .

0

All Articles