gyo*_*dor 20 java android web-services filenotfoundexception
您好,我已经解决了我最初的问题.我是一个完整的Android菜鸟,这是我的第一个应用程序.我正在Android模拟器上测试它.我尝试连接到.NET Web服务http://192.168.3.47/service.asmx
.我得到了FileNotFoundException
.但它就在那里,网址是正确的.我怎么能让他看到这个?
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)
Run Code Online (Sandbox Code Playgroud)
这发生在这里: 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();
Run Code Online (Sandbox Code Playgroud)
Dan*_*yer 62
的HttpURLConnection
类是误导的,因为它会抛出FileNotFoundException
为400或以上的任何HTTP错误代码.
因此,它不一定是不正确的URL(404),它可能是400(错误请求),403(禁止),500(内部服务器错误)或其他.
使用该getResponseCode
方法可以更准确地指出问题.
gpi*_*ese 17
这与我遇到的问题相同:如果您尝试从连接中读取getInputStream(),HttpUrlConnection将返回FileNotFoundException.
当状态代码高于400时,您应该使用getErrorStream().
除此之外,请注意,因为成功状态代码不仅仅是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....
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您将能够在成功和错误情况下获得所需的响应.
希望这可以帮助!
归档时间: |
|
查看次数: |
34800 次 |
最近记录: |