在我的 Android 应用程序中,我正在对数据库进行异步调用以获取体育提要(遵循此链接中的 API 文档:https://www.mysportsfeeds.com/data-feeds/api-docs/(导航到“季节性比赛”部分))。
下面是我的 Android 代码(与我在 Eclipse 中启动的 Java 代码完全相同,它返回了正确的数据):
// Launch an API request to MySportsFeeds to retrieve the schedule for NBA matches
try {
URL url = new URL("https://api.mysportsfeeds.com/v2.1/pull/nfl/current/games.json");
String encoding = java.util.Base64.getEncoder().encodeToString("[I_PUT_THE_CORRECT_API_KEY_HERE]:MYSPORTSFEEDS".getBytes());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + encoding);
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception ex) {
System.out.println("Exception: " + ex.toString());
}
}
这给了我以下错误:
<块引用>java.io.FileNotFoundException: https://api.mysportsfeeds.com/v2.1/pull/nfl/current/games.json
这看起来很奇怪,因为 Android 和 Java 代码完全相同,而且我已经在 AndroidManifest.xml
文件中启用了相关权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
答案 0 :(得分:0)
我遇到了类似的问题,我试过了,它奏效了!!
String basicAuth = "Basic " + Base64.encodeToString("user:password".getBytes(), Base64.NO_WRAP);
因为您是在 android 上编码,所以请在您的 android 编码方法中尝试 Base64.NO_WRAP
。
答案 1 :(得分:0)
删除该行:root n
为我解决了问题。