2012-03-01 77 views
1

我使用Jsoup爲我的Android應用程序之一,我有一個比較簡單的一段代碼,不斷拋出的NullPointerException當我打電話connection.get()。Jsoup在安卓上Connection.get拋出NullPointerException異常()模擬器

這用來工作一兩個月回來,但安裝最新的SDK後,不再在模擬器中工作。它在我的物理設備上工作正常,但我不知道這是否只是一個模擬錯誤或可能影響其他手機。

這是我的代碼:

String URL = "http://finance.google.com/finance/feeds/default/portfolios?returns=true" 
String _AUTHPARAMS = "GoogleLogin auth=" 
String AuthToken = "random string of characters" 

Connection c = Jsoup.connect(URL); 
c.header("Authorization", _AUTHPARAMS + AuthToken); 
c.userAgent("foo"); 
c.timeout(10000); 
c.ignoreContentType(true); 
Document doc = c.get(); 

我趕上最後一行一個NullPointerException(c.get())

如果我叫exception.getMessage(),它只是說「空」。我不確定除了android或jsoup中的錯誤之外,還有什麼可能導致這種情況?

=======編輯========

我使用Jsoup 1.6.1

下面是從日食堆棧跟蹤:

03-01 00:43:22.459: E/AndroidRuntime(392): FATAL EXCEPTION: AsyncTask #1 
03-01 00:43:22.459: E/AndroidRuntime(392): java.lang.RuntimeException: An error occured while executing doInBackground() 
03-01 00:43:22.459: E/AndroidRuntime(392): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
03-01 00:43:22.459: E/AndroidRuntime(392): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
03-01 00:43:22.459: E/AndroidRuntime(392): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
03-01 00:43:22.459: E/AndroidRuntime(392): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
03-01 00:43:22.459: E/AndroidRuntime(392): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
03-01 00:43:22.459: E/AndroidRuntime(392): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
03-01 00:43:22.459: E/AndroidRuntime(392): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
03-01 00:43:22.459: E/AndroidRuntime(392): at java.lang.Thread.run(Thread.java:1096) 
03-01 00:43:22.459: E/AndroidRuntime(392): Caused by: java.lang.NullPointerException 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.readChunkSize(HttpURLConnectionImpl.java:404) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.<init>(HttpURLConnectionImpl.java:340) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getContentStream(HttpURLConnectionImpl.java:1175) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1754) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:381) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143) 
03-01 00:43:22.459: E/AndroidRuntime(392): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132) 
03-01 00:43:22.459: E/AndroidRuntime(392): at com.karwosts.PortfolioMap.GoogleFinance.SendHttpRequest(GoogleFinance.java:138) 
03-01 00:43:22.459: E/AndroidRuntime(392): at com.karwosts.PortfolioMap.GoogleFinance.getDocument(GoogleFinance.java:78) 
03-01 00:43:22.459: E/AndroidRuntime(392): at com.karwosts.PortfolioMap.GoogleFinance.GetPortfolioList(GoogleFinance.java:36) 
03-01 00:43:22.459: E/AndroidRuntime(392): at com.karwosts.PortfolioMap.GooglePortfolioSelector$AsyncPortfolioRetriever.doInBackground(GooglePortfolioSelector.java:134) 
03-01 00:43:22.459: E/AndroidRuntime(392): at com.karwosts.PortfolioMap.GooglePortfolioSelector$AsyncPortfolioRetriever.doInBackground(GooglePortfolioSelector.java:1) 
03-01 00:43:22.459: E/AndroidRuntime(392): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
03-01 00:43:22.459: E/AndroidRuntime(392): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
03-01 00:43:22.459: E/AndroidRuntime(392): ... 4 more 
+0

附加JSoup的堆棧跟蹤和版本您使用的? – Jens 2012-03-01 08:32:00

+0

@Jens信息添加到帖子,謝謝。 – Tim 2012-03-01 08:56:10

回答

0

從您的堆棧跟蹤我猜你在Android 2.1(或其他的「舊」版本)遇到此bug (15554) - 你應該能夠下載使用Apache HTTP客戶端的文件 - 這更好的工作了很多較舊的Android實現(因爲HttpURLConnection是/是狗屎)。

+0

不知道如果相同的錯誤(我是在2.2有線以太網連接),但我還是把你的意見反正與Apache HttpClient的替代,它似乎工作。感謝您的建議。 – Tim 2012-03-01 19:20:26

+0

那麼,谷歌[推薦HttpURLConnection類](http://android-developers.blogspot.com/2011/09/androids-http-clients.html)3.0及更高版本,但它是一種在2.X版本的越野車。 – Jens 2012-03-02 07:38:34

相關問題