2011-03-03 138 views
19

你好我过去我最初problem。我是一个完全的android noob,这是我的第一个应用程序。我正在Android模拟器上测试这个。我尝试连接到一个.NET web服务http://192.168.3.47/service.asmx。 我收到FileNotFoundException。但它在那里,网址是正确的。我怎么能让他看到?FileNotFoundException异常时调用webservice的


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) 

这发生在这里: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(); 

回答

56

HttpURLConnection类是误导的,因为它会抛出一个FileNotFoundException的任何HTTP错误代码为400或更高。

所以它不一定是一个不正确的URL(404),也可能是400(错误的请求),403(禁止),500(内部服务器错误),或别的东西。

使用getResponseCode方法来获取问题的更精确指示。

+0

任何理由如果响应代码实际上是400,我会得到这个错误?这么奇怪! – 2011-03-08 16:16:08

+0

HTTP错误代码400是“错误的请求”。HttpURLConnection将为400或更高版本引发FileNotFoundException。 – 2011-03-08 16:23:24

+5

很好的回答。另外,如果你得到400或更高,并且服务器也发送一个正文给响应,你可以用'conn.getErrorStream()'方法得到它。 – 2013-08-09 16:32:00

2

确保您可以从您的网络浏览器http://192.168.3.47/service.asmx

访问此网址,并确保没有使用Web浏览器配置代理服务器,如果是这样配置您的代码相应

+0

我确定。我可以从android的web浏览器访问网址。我忘了提及它来自仿真器。模拟器是否使用代理?我在哪里检查? – 2011-03-03 09:54:06

+1

'并确保没有使用您的网络浏览器配置代理? – 2011-03-03 09:55:30

+0

我在代理的模拟器中找不到任何东西,主机不使用代理。 – 2011-03-03 10:02:11

2

尝试删除:

connection.setDoInput(true); 
connection.setDoOutput(true); 
+0

我在上面添加了两行,并且已经解决了移动数据。 – 2016-11-20 07:03:10

14

这是我有同样的问题: HttpURLConnection的返回FileNotFoundException异常,如果你试图从连接读取的getInputStream()。
您应该改用getErrorStream()时的状态代码高于400

比这更多,请小心,因为它不仅是200是成功状态码,甚至201,204,等往往用作成功状态。

下面是我如何去管理它

... 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.... 

这样一个例子,你将能够获得成功和错误的情况下所需要的响应。

希望这有助于!

+1

完美答案+1 – 2016-05-09 15:08:27

0

我同时调用API有这个错误。我意识到我错过了API密钥。任何发现此线程的人都会遇到同样的问题,如果API需要,请不要忘记将API密钥包含在API调用中。

相关问题