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 h
Make sure you can access this URL from your web browser http://192.168.3.47/service.asmx
and make sure there is no proxy configured with your web browser, if so configure your code accordingly
Try removing:
connection.setDoInput(true);
connection.setDoOutput(true);
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!
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.
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.