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 上