FileNotFoundException异常调用W​​ebService时, [英] FileNotFoundException when calling webservice

查看:517
本文介绍了FileNotFoundException异常调用W​​ebService时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我过去我最初的问题。我是一个总的安卓小白,这是我的第一个应用程序。我在Android模拟器测试这一点。我尝试连接到一个.NET Web服务在 http://192.168.3.47/service.asmx 。  我得到一个 FileNotFoundException异常。但它的存在,网址是正确的。我怎样才能让他看到了吗?

 
十一月3日至3号:23:49.741:WARN / System.err的(455):java.io.FileNotFoundException:http://192.168.3.47/service.asmx
十一月3日至3号:23:49.751:WARN / System.err的(455):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
十一月3日至3号:23:49.801:WARN / System.err的(455):在gyozo.HelloWorld.HelloActivity.onClick(HelloActivity.java:62)
十一月3日至3号:23:49.831:WARN / System.err的(455):在android.view.View.performClick(View.java:2485)
十一月3日至3号:23:49.851:WARN / System.err的(455):在android.view.View $ PerformClick.run(View.java:9080)
十一月3日至3号:23:49.871:WARN / System.err的(455):在android.os.Handler.handleCallback(Handler.java:587)
十一月3日至3号:23:49.910:WARN / System.err的(455):在android.os.Handler.dispatchMessage(Handler.java:92)
十一月3日至3号:23:49.940:WARN / System.err的(455):在android.os.Looper.loop(Looper.java:123)
十一月3日至3号:23:49.950:WARN / System.err的(455):在android.app.ActivityThread.main(ActivityThread.java:3683)
十一月3日至3号:23:50.010:WARN / System.err的(455):在java.lang.reflect.Method.invokeNative(本机方法)
十一月3日至3号:23:50.050:WARN / System.err的(455):在java.lang.reflect.Method.invoke(Method.java:507)
十一月3日至3号:23:50.070:WARN / System.err的(455):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
十一月3日至3号:23:50.090:WARN / System.err的(455):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
十一月3日至3号:23:50.110:WARN / System.err的(455):在dalvik.system.NativeStart.main(本机方法)
 

这发生在这里:的InputStream是= connection.getInputStream();

 
网址URL =新的URL(http://192.168.3.47/service.asmx);
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
connection.setRequestMethod(POST);
connection.setRequestProperty(内容类型,
 应用程序/肥皂+ XML;字符集= UTF-8);

connection.setUseCaches(假);
connection.setDoInput(真正的);
connection.setDoOutput(真正的);

字符串SOA prequest =的String.Format(的getText(R.string.ws_listemain_ds_new)的ToString(),市,关键词);
connection.setRequestProperty(内容长度,Integer.toString(SOA prequest.getBytes(UTF-8)的长度)。);
//发送请求
OutputStreamWriter OWR =新OutputStreamWriter(connection.getOutputStream(),UTF-8);

owr.write(SOA prequest);
owr.flush();
owr.close();

//获取响应
InputStream的是= connection.getInputStream();
 

解决方案

的HttpURLConnection 类是误导性的,因为它会抛出一个 FileNotFoundException异常为400或以上的任何HTTP错误code。

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

使用的<一个href="http://developer.android.com/reference/java/net/HttpURLConnection.html#getResponse$c$c%28%29"><$c$c>getResponse$c$c方法获取问题的更precise指示。

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

解决方案

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.

这篇关于FileNotFoundException异常调用W​​ebService时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆