gpt4 book ai didi

android - Android 中带有 HttpURLConnection 的 FileNotFoundException

转载 作者:可可西里 更新时间:2023-11-01 16:43:35 24 4
gpt4 key购买 nike

每当我尝试在 Android 中运行单元测试时,我都会收到以下错误 - 在 Android 外部运行时通过的测试:

07-18 15:43:44.816: W/System.err(545): uk.co.redfruit.libraries.srpDB.exceptions.SRBClientException: java.io.FileNotFoundExce$
07-18 15:43:44.816: W/System.err(545): at uk.co.redfruit.libraries.srpDB.SRPDBClient.getContent(SRPDBClient.java:116)
07-18 15:43:44.816: W/System.err(545): at uk.co.monkeypower.android.straightrazordatabase.test.SRPDBClientTest.testGetConten$
07-18 15:43:44.826: W/System.err(545): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 15:43:44.826: W/System.err(545): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 15:43:44.826: W/System.err(545): at junit.framework.TestCase.runTest(TestCase.java:154)
07-18 15:43:44.826: W/System.err(545): at junit.framework.TestCase.runBare(TestCase.java:127)
07-18 15:43:44.826: W/System.err(545): at junit.framework.TestResult$1.protect(TestResult.java:106)
07-18 15:43:44.826: W/System.err(545): at junit.framework.TestResult.runProtected(TestResult.java:124)
07-18 15:43:44.826: W/System.err(545): at junit.framework.TestResult.run(TestResult.java:109)
07-18 15:43:44.826: W/System.err(545): at junit.framework.TestCase.run(TestCase.java:118)
07-18 15:43:44.826: W/System.err(545): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
07-18 15:43:44.826: W/System.err(545): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
07-18 15:43:44.826: W/System.err(545): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
07-18 15:43:44.836: W/System.err(545): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
07-18 15:43:44.836: W/System.err(545): Caused by: java.io.FileNotFoundException: http://straightrazorplace.com/srpwiki/index.$
07-18 15:43:44.836: W/System.err(545): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getIn$
07-18 15:43:44.836: W/System.err(545): at uk.co.redfruit.libraries.srpDB.SRPDBClient.getContent(SRPDBClient.java:106)
07-18 15:43:44.836: W/System.err(545): ... 13 more

我知道在 Android 4.x+ 中似乎有一个错误会导致这种情况发生,但在 Gingerbread 中运行时我会遇到完全相同的错误。

导致错误的代码如下:

StringBuilder stringBuilder = null;
try {
URL url = new URL(targetURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
stringBuilder = new StringBuilder();
String content = null;
while ((content = reader.readLine()) != null) {
stringBuilder.append(content + "\n");
}
reader.close();
} catch (IOException e) {
throw new SRBClientException(e);
}

我的 list 中有 INTERNET 权限,并且 url 确实存在 - 我可以在浏览器中访问它。任何人都可以看到我哪里出错了或者我遗漏了什么吗?

更新:我尝试使用 basic http client作为尝试的解决方法。它没有用,但它提供了一个完全不同的错误。它表明我得到了一个 403,在之前的尝试中,它只是导致了 FileNotFound,但这次也导致了 JSON 异常,因为返回的 html 错误消息不能(显然)被解析为 JSON。

我猜这是一个服务器端问题,虽然我对为什么我在 Android 中得到 403 感到困惑,但在其他情况下没有...

最佳答案

事实证明,无论默认用户代理是什么(也许什么都没有?),服务器正在阻止并返回 403。

通过将其设置为标准浏览器用户代理(在本例中我使用的是 Chrome),它起作用了。

关于android - Android 中带有 HttpURLConnection 的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17726866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com