【问题标题】:java.io.FileNotFoundException when I use Get Method in an HTTP Request当我在 HTTP 请求中使用 Get 方法时出现 java.io.FileNotFoundException
【发布时间】:2013-11-07 17:56:47
【问题描述】:

我是 Android 开发的初学者。我正在尝试开发一个管理 FB 事件的小型应用程序。当我尝试从 Graphfh 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 释放 800K,51% 释放 3320K/6727K,外部 1660K/2137K,暂停 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): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521) 11-07 18:40:23.726: W/System.err(418): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:258) 11-07 18:40:23.726: W/System.err(418): 在 it.unisannio.flash.mobile.MainFragment.onSessionStateChange(MainFragment.java:79) 11-07 18:40:23.726: W/System.err(418): 在 it.unisannio.flash.mobile.MainFragment.access$0(MainFragment.java:68) 11-07 18:40:23.726: W/System.err(418): 在 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): 在 android.os.Handler.handleCallback(Handler.java:587) 11-07 18:40:23.726: W/System.err(418): 在 android.os.Handler.dispatchMessage(Handler.java:92) 11-07 18:40:23.726: W/System.err(418): 在 android.os.Looper.loop(Looper.java:130) 11-07 18:40:23.726: W/System.err(418): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 11-07 18:40:23.726: W/System.err(418): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-07 18:40:23.726: W/System.err(418): 在 java.lang.reflect.Method.invoke(Method.java:507) 11-07 18:40:23.726: W/System.err(418): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 11-07 18:40:23.735: W/System.err(418): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 11-07 18:40:23.735: W/System.err(418): 在 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): 窗口已经聚焦,忽略焦点增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@40761408 11-07 18:40:29.314: D/dalvikvm(295): GC_EXPLICIT 释放 637K,40% 释放 7291K/12039K,外部 1625K/2137K,暂停 104ms 11-07 18:41:27.998: I/dalvikvm(295): Jit: 将 JitTable 的大小从 2048 调整为 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();
        }

【问题讨论】:

  • 先在浏览器中查看网址。

标签: java android facebook events


【解决方案1】:

显然org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl,这是您运行时环境中使用的HttpURLConnection 的实现,当响应status code 为400 及以上(错误)时,将抛出FileNotFoundException
可以在这个类的源码中看到:

/*
* 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 状态代码(错误请求),并出现以下错误:

{
   "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 API documentation 中阅读更多关于需要做什么的信息

【讨论】:

猜你喜欢
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 2016-10-10
  • 2019-05-07
相关资源
最近更新 更多