当我在HTTP请求中使用Get Method时出现java.io.FileNotFoundException [英] java.io.FileNotFoundException when I use Get Method in an HTTP Request

查看:720
本文介绍了当我在HTTP请求中使用Get Method时出现java.io.FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的初学者.我正在尝试开发一个管理FB Event的小型应用程序.当我尝试从Grapfh API获取事件时遇到了这个问题

I'm a beginner with Android development. I'm trying to develop a small application that manages FB Event. I have this problem when I try to get the events from the Grapfh API

这是日志:



11-07 18:40:22.785: D/dalvikvm(418): GC_CONCURRENT freed 634K, 51% free 3331K/6727K, external 1660K/2137K, paused 4ms+5ms
11-07 18:40:23.235: D/dalvikvm(418): GC_CONCURRENT freed 800K, 51% free 3320K/6727K, external 1660K/2137K, paused 4ms+3ms
11-07 18:40:23.595: D/dalvikvm(418): GC_CONCURRENT freed 686K, 52% free 3275K/6727K, external 1660K/2137K, paused 4ms+3ms
11-07 18:40:23.715: W/System.err(418): java.io.FileNotFoundException: https://graph.facebook.com/me
11-07 18:40:23.726: W/System.err(418):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
11-07 18:40:23.726: W/System.err(418):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:258)
11-07 18:40:23.726: W/System.err(418):  at it.unisannio.flash.mobile.MainFragment.onSessionStateChange(MainFragment.java:79)
11-07 18:40:23.726: W/System.err(418):  at it.unisannio.flash.mobile.MainFragment.access$0(MainFragment.java:68)
11-07 18:40:23.726: W/System.err(418):  at it.unisannio.flash.mobile.MainFragment$1.call(MainFragment.java:116)
11-07 18:40:23.726: W/System.err(418):  at com.facebook.Session$3$1.run(Session.java:1302)
11-07 18:40:23.726: W/System.err(418):  at android.os.Handler.handleCallback(Handler.java:587)
11-07 18:40:23.726: W/System.err(418):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 18:40:23.726: W/System.err(418):  at android.os.Looper.loop(Looper.java:130)
11-07 18:40:23.726: W/System.err(418):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-07 18:40:23.726: W/System.err(418):  at java.lang.reflect.Method.invokeNative(Native Method)
11-07 18:40:23.726: W/System.err(418):  at java.lang.reflect.Method.invoke(Method.java:507)
11-07 18:40:23.726: W/System.err(418):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-07 18:40:23.735: W/System.err(418):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-07 18:40:23.735: W/System.err(418):  at dalvik.system.NativeStart.main(Native Method)
11-07 18:40:25.314: D/dalvikvm(418): GC_CONCURRENT freed 621K, 52% free 3280K/6727K, external 2046K/2137K, paused 4ms+3ms
11-07 18:40:27.314: W/InputManagerService(60): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40761408
11-07 18:40:29.314: D/dalvikvm(295): GC_EXPLICIT freed 637K, 40% free 7291K/12039K, external 1625K/2137K, paused 104ms
11-07 18:41:27.998: I/dalvikvm(295): Jit: resizing JitTable from 2048 to 4096

这是代码:

try {
          URL url = new URL("https://graph.facebook.com/me");
          HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
          BufferedReader read = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          String line = read.readLine();

          String html = "";
          while(line!=null) {
            html += line;
            line = read.readLine();
          }
        userInfoTextView.setText(line);
        } catch(MalformedURLException ex) {
                ex.printStackTrace();
        } catch(IOException ioex) {
                ioex.printStackTrace();
        }

推荐答案

显然,org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl(在您的运行时环境中使用的HttpURLConnection的实现)在响应

Apparently org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl, which is the implementation of HttpURLConnection used in your runtime environment will throw a FileNotFoundException when the response status code is 400 and above (errors).
You can see it in the source code of this class:

/*
* if the requested file does not exist, throw an exception formerly the
* Error page from the server was returned if the requested file was
* text/html this has changed to return FileNotFoundException for all
* file types
*/
if (responseCode >= HTTP_BAD_REQUEST) {
   throw new FileNotFoundException(url.toString());
}

以您为例,如果您要检查从该URL返回的响应,则会看到获得 400 状态代码(错误请求),并显示以下错误:

In your case, if you will check the response returned from this URL you will see that you get a 400 status code (Bad Request) with the following error:

{
   "error": {
      "message": "An active access token must be used to query information about the current user.",
      "type": "OAuthException",
      "code": 2500
   }
}

您可以通过调用 getResponseCode():

int status = connection .getResponseCode();

您可以在Facebook APIs 文档

You can read more about what needs to be done in the Facebook APIs documentation

这篇关于当我在HTTP请求中使用Get Method时出现java.io.FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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