Question

I have a class called RetreiveHttpStringResponse. It's used to get an InputStream from an URL containing JSON data. The class extends AsyncTask<String, Void, InputStream>. So the strange problem here is that null is always returned. No matter what. There is even no Exception. I checked out the program behaviour with the debugger and could see that at point (1) the processing is jumping immediately to the finally-statement and continues with return null;. And again there are no Errors and no Exceptions are going on. The programm is running normally. I'm using Android 4.4 (SDK version 19), the response code is 200 and the following lines are set in the Manifest file.

  • uses-permission android:name="android.permission.INTERNET"
  • uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"

The problem is happening on the emulator and on a real device with internet connection. Here is the code:

@Override
protected InputStream doInBackground(String... arg0) {
    URL url = null;
    InputStream is = null;
    HttpURLConnection urlConn = null;
    int responseCode = 0;

    try {
        url = new URL(arg0[0]);
        urlConn = (HttpURLConnection) url.openConnection();
        urlConn.setReadTimeout(10000);
        urlConn.setConnectTimeout(15000);
        urlConn.setRequestMethod("GET");
        urlConn.connect();

        responseCode = urlConn.getResponseCode();
        Log.d("DataHandlerInternet:RESPONSE_CODE", "The response is: " + responseCode);

        is= urlConn.getInputStream(); //-->(1)<--
        return is;
    }
    catch ( MalformedURLException e ) { // new URL() went wrong!
        //TODO error message. URL is not correct!
        e.printStackTrace();
    }
    catch (SocketTimeoutException e) { // Timeout while connecting or holding connection to URL.
        //TODO error message. Timeout happened!
        e.printStackTrace();
    }
    catch ( IOException e ) { // openConnection() failed!
        //TODO error message. Couldn't connect to URL!
        e.printStackTrace();
    }
    catch( Exception e ) { // Any other Exception!
        e.printStackTrace();
    }
    finally {
        try { if(is != null) { is.close(); } } catch(Exception e) {e.printStackTrace();}
        try { if(urlConn != null) { urlConn.disconnect(); } } catch(Exception e) {e.printStackTrace();}
    }

    return null;
}

One bad solution is to delete the finally-statement. Well, not the best way to solve this problem. Now I changed the code. I've put the reading in it and return just the String.

@Override
protected String doInBackground(String... arg0) {
    URL url = null;
    InputStream is = null;
    HttpURLConnection urlConn = null;
    int responseCode = 0;

    try {
        url = new URL(arg0[0]);
        urlConn = (HttpURLConnection) url.openConnection();
        urlConn.setReadTimeout(10000);
        urlConn.setConnectTimeout(15000);
        urlConn.setRequestMethod("GET");
        urlConn.connect();

        responseCode = urlConn.getResponseCode();
        Log.d("DataHandlerInternet:RESPONSE_CODE", "The response is: " + responseCode);

        is= urlConn.getInputStream();
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while (  (line = br.readLine()) != null ) {
            sb.append(line);
        }
        return sb.toString();
    }
    catch ( MalformedURLException e ) { // new URL() went wrong!
        //TODO error message. URL is not correct!
        e.printStackTrace();
    }
    catch (SocketTimeoutException e) { // Timeout while connecting or holding connection to URL.
        //TODO error message. Timeout happened!
        e.printStackTrace();
    }
    catch ( IOException e ) { // openConnection() failed!
        //TODO error message. Couldn't connect to URL!
        e.printStackTrace();
    }
    catch( Exception e ) { // Any other Exception!
        e.printStackTrace();
    }
    finally {
        try { if(is != null) { is.close(); } } catch(Exception e) {e.printStackTrace();}
        try { if(urlConn != null) { urlConn.disconnect(); } } catch(Exception e) {e.printStackTrace();}
    }

    return null;
}

And still, after going through the while loop the return line; is completely ignored. I've checked the data in the String with the debugger and it was correct! No Errors no Exceptions.

Was it helpful?

Solution 2

I dont see why you get your null result but, one thing you are doing wrong is actually returning InputStream:

    is= urlConn.getInputStream(); //-->(1)<--
    return is;

you should read your stream in doInBackground (on worker thread), otherwise reading it in onPostExecute (UI Thread), will possibly cause NetworkOnMainThreadException, or at least ANR. Reading data from InputStream is still a network operation - data you download can be several MBs.

OTHER TIPS

finally will run in either case, also during normal return without exceptions. And you call .close in the finally statement clause.

So your code always returns the closed stream. Probably this is not that you intend.

Your description ("jumps to finally statement") still looks very much like a exception has been thrown by urlConn.getInputStream(). Strange you do not observe it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top