gpt4 book ai didi

java - getResponseCode 抛出 NPE

转载 作者:行者123 更新时间:2023-12-01 04:43:40 24 4
gpt4 key购买 nike

HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(10000);
urlConnection.setDoInput(true);

try {
int statusCode = urlConnection.getResponseCode();

最后一行,方法 getResponseCode 抛出 NPE:

04-22 15:59:28.704: E/sreda(1627): java.lang.NullPointerException
04-22 15:59:28.704: E/sreda(1627): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279)
04-22 15:59:28.704: E/sreda(1627): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.readChunkSize(HttpURLConnectionImpl.java:404)
04-22 15:59:28.704: E/sreda(1627): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.<init>(HttpURLConnectionImpl.java:340)
04-22 15:59:28.704: E/sreda(1627): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getContentStream(HttpURLConnectionImpl.java:1175)
04-22 15:59:28.704: E/sreda(1627): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1754)
04-22 15:59:28.704: E/sreda(1627): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
04-22 15:59:28.704: E/sreda(1627): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)
04-22 15:59:28.704: E/sreda(1627): at my.pack.service.DataService.get(DataService.java:184)
04-22 15:59:28.704: E/sreda(1627): at my.pack.service.DataService.getWithRefresh(DataService.java:148)
04-22 15:59:28.704: E/sreda(1627): at my.pack.service.DataService.getRemoteContacts(DataService.java:355)
04-22 15:59:28.704: E/sreda(1627): at my.pack.activity.ContactsActivity$3.doInBackground(ContactsActivity.java:247)
04-22 15:59:28.704: E/sreda(1627): at my.pack.activity.ContactsActivity$3.doInBackground(ContactsActivity.java:1)
04-22 15:59:28.704: E/sreda(1627): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-22 15:59:28.704: E/sreda(1627): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-22 15:59:28.704: E/sreda(1627): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-22 15:59:28.704: E/sreda(1627): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-22 15:59:28.704: E/sreda(1627): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-22 15:59:28.704: E/sreda(1627): at java.lang.Thread.run(Thread.java:1096)

UPD 在 Android 4.0 上没有异常(exception),但在 Android 2.2 上

最佳答案

您可能会遇到与 this question 中相同的问题,其中出现类似的堆栈跟踪...长话短说:对于旧的 Android 版本,使用 Apache HttpClient 会更安全。

关于java - getResponseCode 抛出 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16146875/

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