java – FileNotFoundException when calling webservice-ThrowExceptions

Exception or error:

Hello I got past my initial problem. I’m a total android noob, this is my first app. I’m testing this on the Android emulator. I try to connect to a .NET webservice at
I get a FileNotFoundException. But it IS there, the url is correct. How can I make him see that?

03-03 11:23:49.741: WARN/System.err(455):
03-03 11:23:49.751: WARN/System.err(455):     at
03-03 11:23:49.801: WARN/System.err(455):     at gyozo.HelloWorld.HelloActivity.onClick(
03-03 11:23:49.831: WARN/System.err(455):     at android.view.View.performClick(
03-03 11:23:49.851: WARN/System.err(455):     at android.view.View$
03-03 11:23:49.871: WARN/System.err(455):     at android.os.Handler.handleCallback(
03-03 11:23:49.910: WARN/System.err(455):     at android.os.Handler.dispatchMessage(
03-03 11:23:49.940: WARN/System.err(455):     at android.os.Looper.loop(
03-03 11:23:49.950: WARN/System.err(455):     at
03-03 11:23:50.010: WARN/System.err(455):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 11:23:50.050: WARN/System.err(455):     at java.lang.reflect.Method.invoke(
03-03 11:23:50.070: WARN/System.err(455):     at$
03-03 11:23:50.090: WARN/System.err(455):     at
03-03 11:23:50.110: WARN/System.err(455):     at dalvik.system.NativeStart.main(Native Method)

This happens here: InputStream is = connection.getInputStream();

URL url = new URL("");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
 "application/soap+xml; charset=utf-8");


String soapRequest = String.format(getText(R.string.ws_listemain_ds_new).toString(), city, keyword);
connection.setRequestProperty("Content-Length", Integer.toString(soapRequest.getBytes("UTF-8").length));
//Send request
OutputStreamWriter owr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");


//Get Response  
InputStream is = connection.getInputStream();
How to solve:

The HttpURLConnection class is misleading in that it will throw a FileNotFoundException for any HTTP error code of 400 or above.

So it’s not necessarily an incorrect URL (404) it could be 400 (bad request), 403 (forbidden), 500 (internal server error) or something else.

Use the getResponseCode method to get a more precise indication of the problem.


It’s the same problem I was having:
HttpUrlConnection returns FileNotFoundException if you try to read the getInputStream() from the connection.
You should instead use getErrorStream() when the status code is higher than 400.

More than this, please be careful since it’s not only 200 to be the success status code, even 201, 204, etc. are often used as success statuses.

Here is an example of how I went to manage it

... connection code code code ...

// Get the response code 
int statusCode = connection.getResponseCode();

InputStream is = null;

if (statusCode >= 200 && statusCode < 400) {
   // Create an InputStream in order to extract the response object
   is = connection.getInputStream();
else {
   is = connection.getErrorStream();

... callback/response to your handler....

In this way, you’ll be able to get the needed response in both success and error cases.

Hope this helps!


Try removing:



Make sure you can access this URL from your web browser

and make sure there is no proxy configured with your web browser, if so configure your code accordingly


I had this error while calling the API. I realized that I was missing the API key. Anyone finding this thread for the same problem, don’t forget to include the API key in the API call, if it is required by the API.

Leave a Reply

Your email address will not be published. Required fields are marked *