FileNotFoundException when calling webservice
Asked Answered
O

5

21

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 http://192.168.3.47/service.asmx. 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): java.io.FileNotFoundException: http://192.168.3.47/service.asmx
03-03 11:23:49.751: WARN/System.err(455):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
03-03 11:23:49.801: WARN/System.err(455):     at gyozo.HelloWorld.HelloActivity.onClick(HelloActivity.java:62)
03-03 11:23:49.831: WARN/System.err(455):     at android.view.View.performClick(View.java:2485)
03-03 11:23:49.851: WARN/System.err(455):     at android.view.View$PerformClick.run(View.java:9080)
03-03 11:23:49.871: WARN/System.err(455):     at android.os.Handler.handleCallback(Handler.java:587)
03-03 11:23:49.910: WARN/System.err(455):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 11:23:49.940: WARN/System.err(455):     at android.os.Looper.loop(Looper.java:123)
03-03 11:23:49.950: WARN/System.err(455):     at android.app.ActivityThread.main(ActivityThread.java:3683)
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(Method.java:507)
03-03 11:23:50.070: WARN/System.err(455):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-03 11:23:50.090: WARN/System.err(455):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
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("http://192.168.3.47/service.asmx");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", 
 "application/soap+xml; charset=utf-8");

connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);

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");

owr.write(soapRequest);
owr.flush();
owr.close();

//Get Response  
InputStream is = connection.getInputStream();
Oversee answered 3/3, 2011 at 9:41 Comment(0)
P
62

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.

Panorama answered 3/3, 2011 at 11:31 Comment(3)
Any reason I'd be getting this error if the response code is actually 400? So strange!Depressed
HTTP error code 400 is "Bad Request". HttpURLConnection will throw a FileNotFoundException for 400 or above.Panorama
Great answer. Also, if you get 400 and above and the server also sends a body to the response, you can get that with the conn.getErrorStream() method.Albi
A
16

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!

Amazon answered 29/6, 2015 at 18:26 Comment(0)
H
4

Try removing:

connection.setDoInput(true);
connection.setDoOutput(true);
Heeling answered 23/7, 2012 at 9:32 Comment(1)
I have added above two line and It has been solved for mobile data.Windrow
S
2

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

Stacee answered 3/3, 2011 at 9:43 Comment(7)
I made sure. I can access the url from the web browser from android. I forget to mention it is from the emulator. Does the emulator use proxy? Where do I check that?Oversee
and make sure there is no proxy configured with your web browser ?Stacee
I can't find anything in the emulator for proxies, the host computer does not use proxy.Oversee
make sure there must be proxy configured with your web browserStacee
There is no proxy configured for my browser or for my computer.Oversee
I'm sorry I don't understand. I don't want to download the file. I want to send a soap request to 192.168.3.47/service.asmx and call a web service method.Oversee
ahh. yes thats my mistake, you can't proceed by downloading it.Stacee
C
0

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.

Conker answered 6/4, 2016 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.