Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundException when calling webservice

Tags:

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(); 
like image 219
gyozo kudor Avatar asked Mar 03 '11 09:03

gyozo kudor


People also ask

How do I resolve FileNotFoundException?

How to Fix FileNotFoundException. Since FileNotFoundException is a checked exception, a try-catch block should be used to handle it. The try block should contain the lines of code that can throw the exception and the catch block should catch and handle the exception appropriately.

When should I use FileNotFoundException?

As indicated on Java's API documentation, this exception can be thrown when: A file with the specified pathname does not exist. A file with the specified pathname does exist but is inaccessible for some reason (requested writing for a read-only file, or permissions don't allow accessing the file)

How do I stop FileNotFoundException in Java?

How to avoid FileNotFoundException in java? Please ensure file exists when you try to read from file from path exists (using FileInputStream) to avoid FileNotFoundException. Please ensure file exists when we try to acces file from invalid path using RandomAccessFile to avoid FileNotFoundException.

What is System IO FileNotFoundException?

FileNotFoundException(String, String, Exception) Initializes a new instance of the FileNotFoundException class with a specified error message, the file name that cannot be found, and a reference to the inner exception that is the cause of this exception.


1 Answers

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.

like image 102
Dan Dyer Avatar answered Oct 11 '22 06:10

Dan Dyer