droid模拟器抛出NullPointerException异常

droid模拟器抛出NullPointerException异常

本文介绍了Jsoup在Android模拟器抛出NullPointerException异常上Connection.get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Jsoup为我的Andr​​oid应用程序之一,我有一个比较简单的一张code那不断抛出的NullPointerException当我打电话connection.get()。

这用来工作一两个月回来,但安装最新的SDK后,不再在模拟器中工作。这是我工作的物理设备上很好,但我不知道这是一个仿真仅错误或可能会影响其他手机。

这是我的code:

 字符串的URL =htt​​p://finance.google.com/finance/feeds/default/portfolios?returns=true
字符串_AUTHPARAMS =的GoogleLogin AUTH =
字符串的authToken =字符的随机字符串连接C = Jsoup.connect(URL);
c.header(授权,_AUTHPARAMS +的authToken);
c.userAgent(富);
c.timeout(10000);
c.ignoreContentType(真);
文档的文档= c.get();

我赶上最后一行一个NullPointerException异常(c.get())

如果我叫exception.getMessage(),它只是说空。我不知道还有什么可以比Android或jsoup一个bug会导致这等?

=======编辑========

我使用Jsoup 1.6.1

下面是Eclipse中的堆栈跟踪:

  03-01 00:43:22.459:E / AndroidRuntime(392):致命异常:AsyncTask的#1
03-01 00:43:22.459:E / AndroidRuntime(392):了java.lang.RuntimeException:执行doInBackground发生错误()
03-01 00:43:22.459:E / AndroidRuntime(392):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
03-01 00:43:22.459:E / AndroidRuntime(392):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
03-01 00:43:22.459:E / AndroidRuntime(392):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-01 00:43:22.459:E / AndroidRuntime(392):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
03-01 00:43:22.459:E / AndroidRuntime(392):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-01 00:43:22.459:E / AndroidRuntime(392):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-01 00:43:22.459:E / AndroidRuntime(392):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:561)
03-01 00:43:22.459:E / AndroidRuntime(392):在java.lang.Thread.run(Thread.java:1096)
03-01 00:43:22.459:E / AndroidRuntime(392):致:显示java.lang.NullPointerException
03-01 00:43:22.459:E / AndroidRuntime(392):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.readChunkSize(HttpURLConnectionImpl.java:404)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.<init>(HttpURLConnectionImpl.java:340)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getContentStream(HttpURLConnectionImpl.java:1175)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1754)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponse$c$c(HttpURLConnectionImpl.java:1374)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:381)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:364)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
03-01 00:43:22.459:E / AndroidRuntime(392):在org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
03-01 00:43:22.459:E / AndroidRuntime(392):在com.karwosts.PortfolioMap.GoogleFinance.SendHtt prequest(GoogleFinance.java:138)
03-01 00:43:22.459:E / AndroidRuntime(392):在com.karwosts.PortfolioMap.GoogleFinance.getDocument(GoogleFinance.java:78)
03-01 00:43:22.459:E / AndroidRuntime(392):在com.karwosts.PortfolioMap.GoogleFinance.GetPortfolioList(GoogleFinance.java:36)
03-01 00:43:22.459:E / AndroidRuntime(392):在com.karwosts.PortfolioMap.GooglePortfolioSelector$AsyncPortfolioRetriever.doInBackground(GooglePortfolioSelector.java:134)
03-01 00:43:22.459:E / AndroidRuntime(392):在com.karwosts.PortfolioMap.GooglePortfolioSelector$AsyncPortfolioRetriever.doInBackground(GooglePortfolioSelector.java:1)
03-01 00:43:22.459:E / AndroidRuntime(392):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
03-01 00:43:22.459:E / AndroidRuntime(392):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
03-01 00:43:22.459:E / AndroidRuntime(392):... 4个


解决方案

从你的堆栈跟踪我猜你遇到这样的一个Android 2.1(或其他旧版本) - 你应该能够下载使用Apache HTTP客户端文件 - 这适用于较旧的Andr​​oid实现好了很多(因为 HttpURLConnection类是/是一个马车一块狗屎)

I'm using Jsoup for one of my android applications, and I have a relatively simple piece of code that keeps throwing NullPointerException when I call connection.get().

This used to work a couple months back, but after installing the latest SDK it no longer works in the emulator. It is working fine on my physical device, but I don't know if this is an emulation only bug or could impact other handsets.

This is my code:

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();

I am catching a NullPointerException on the last line (c.get())

If I call exception.getMessage(), it just says "null". I'm not sure what else could be causing this other than a bug in android or jsoup?

======= EDIT ========

I'm using Jsoup 1.6.1

Here's the stack trace from eclipse:

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
解决方案

From your stack trace I'm guessing you're encountering this bug (15554) on an Android 2.1 (or other "old" version) - you should be able to download the document using the Apache HTTP client - which works a lot better on older Android implementations (since HttpURLConnection is/was a buggy piece of shit).

这篇关于Jsoup在Android模拟器抛出NullPointerException异常上Connection.get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:04