HttpURLConnection getInputStream FileNotFoundException

Hello Android/Java coders!

So I recently got some weird errors when doing http requests in my Android game Hamster Climb. I was trying to get the input stream from a HttpURLConnection but kept getting a FileNotFoundException even tho the URL worked perfectly in my desktop browser. Here’s an example, without error catching stuff, of how the code could look like:

String user = getUser();
String url = "http://somesite.com/page?user=" + user;
URL u = new URL(url);

HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setDoInput(true);
c.setDoOutput(true);
c.connect();

InputStream is = c.getInputStream();

The problem was that at the last line the call to getInputStream generated a FileNotFoundException. I printed the URL that was used and tried it in the browser. Worked prefectly.

I managed to solve it tho. The problem was that even tho creating a URL object with the built url string works, the url might be broken. For example, if user is “Some guy” the url will contain a space which is not valid. Browsers solve this by automatically replacing the space with %20 which is what my browser did.

To solve this in code you use the URLEncoder class’ static method encode. This method takes a string and encodes it to a string that is ok to put in a URL. So replacing

String url = "http://somesite.com/page?user=" + user;

with

String url = "http://somesite.com/page?user="
+ URLEncoder.encode(user, "UTF-8");

solves the problem when user contains illegal URL characters.

Hope this helps someone out there getting weird errors. 🙂

Advertisements

10 Responses to “HttpURLConnection getInputStream FileNotFoundException”


  1. 1 rishabh 2011-01-06 at 11:44

    I had the same exact exception happening at my end.
    Though the bug I found was a null value was being set for a request-header property of HttpURLConnection.
    Maybe this helps someone.

  2. 2 yufanly 2011-08-12 at 11:37

    nice

  3. 3 Nate 2011-09-13 at 09:41

    @rishabh,

    which request-header property?

    I am also getting this exception, but I have no characters in my URL string that would need encoding. can you tell me which property you had that was null?

    thanks.

    • 4 Rishabh 2011-09-13 at 10:37

      Hey Nate,
      In my case, the K-V pair for a request property was “site-id” : “”
      So if by mistake a null value was being passed, the server was not able to make a connection, and was throwing a FNFE.
      The best practice states that:
      * Check your request params, for valid K-V pairs.
      * Check for a 200 OK response, before you perform a getInputStream on the connection object.
      if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
      // valid response, retrieve your input stream
      BufferedReader reader = new BufferedReader(new InputStreamReader(
      connection.getInputStream(), “UTF-8”));
      } else {
      // something bad happened, debug from your server .
      }

  4. 5 Rahul Popuri 2011-12-31 at 22:21

    This is awesome thanks! Took me 2 hours to fix this

  5. 6 sankar 2012-03-21 at 10:26

    removing c.setDoOutput(true) worked for me. 🙂

  6. 8 AnatolyKarpov 2012-05-13 at 17:34

    Thank you very much, very useful.

  7. 9 iec2007003 2013-03-06 at 15:54

    Something I want to add here. Different part of a URL has different encoding so you should not encode the whole URL with the same encoding. Only encode the unreliable part of the URL. Here is a stack overflow link to explain this: http://stackoverflow.com/questions/3286067/url-encoding-in-android


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




I am a technology interested programmer. I live and work in Uppsala, Sweden after moving here to study Engineering Physics at Uppsala University.

❤️ cats


%d bloggers like this: