getResponseCode抛出NPE

时间:2013-04-22 12:15:38

标签: java android

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)

Android 4.0上的UPD没有例外,但在Android 2.2上

1 个答案:

答案 0 :(得分:0)

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