我正在尝试使用jsoup编写一个天气刮刮Android应用程序。我有使用Java编写的代码,但即使是我在android中编写的最简单的形式也会引发错误。这是我的代码的一部分:
private class Retrieve_Weather extends AsyncTask<Void, Void, String> {
private final String TAG = "Retrieve_Weather";
protected String doInBackground(Void... params) {
String weatherline= "oTHErly";
Log.i(TAG, weatherline);
Document doc = null;
try {
Log.i(TAG, "trying document...");
doc = Jsoup.connect("http://m.wund.com/cgi-bin/findweather/getForecast?brand=mobile&query=02139").get();
Log.i(TAG, "after document");
weatherline = "newline";
}
catch (IOException except) {
Log.i(TAG, "Cannot Connect", except);
except.printStackTrace();
weatherline = "Can't Connect";
Log.i(TAG, "End stacktrace");
}
Log.i(TAG, weatherline);
return weatherline;
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
@Override
protected void onPostExecute(String result){
//super.onPostExecute(result);
Log.i(TAG, "onPostExecute");
}
}
我已经玩过我的代码和logcat足以弄明白我的错误是在运行.get()时抛出的nullpointerexception。我只是无法弄清楚为什么或如何解决它。这是我的堆栈跟踪:
07-09 03:18:13.901: I/Retrieve_Weather(1754): trying document...
07-09 03:18:15.900: D/dalvikvm(1754): GC_CONCURRENT freed 80K, 7% free 2711K/2912K, paused 83ms+20ms, total 244ms
07-09 03:18:15.910: D/dalvikvm(1754): WAIT_FOR_CONCURRENT_GC blocked 157ms
07-09 03:18:16.351: D/dalvikvm(1754): GC_CONCURRENT freed 297K, 15% free 2841K/3340K, paused 81ms+124ms, total 329ms
07-09 03:18:16.740: W/dalvikvm(1754): Exception Ljava/lang/NullPointerException; thrown while initializing Lorg/jsoup/nodes/Entities;
07-09 03:18:16.740: W/dalvikvm(1754): Exception Ljava/lang/ExceptionInInitializerError; thrown while initializing Lorg/jsoup/nodes/Entities$EscapeMode;
07-09 03:18:16.810: W/System.err(1754): java.util.concurrent.ExecutionException: java.lang.ExceptionInInitializerError
07-09 03:18:16.830: W/System.err(1754): at java.util.concurrent.FutureTask.report(FutureTask.java:94)
07-09 03:18:16.830: W/System.err(1754): at java.util.concurrent.FutureTask.get(FutureTask.java:160)
07-09 03:18:16.860: W/System.err(1754): at android.os.AsyncTask.get(AsyncTask.java:482)
07-09 03:18:16.880: W/System.err(1754): at com.example.weatherresponse.Weather.onCreate(Weather.java:35)
07-09 03:18:16.880: W/System.err(1754): at android.app.Activity.performCreate(Activity.java:5104)
07-09 03:18:16.911: W/System.err(1754): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-09 03:18:16.911: W/System.err(1754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-09 03:18:16.960: W/System.err(1754): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-09 03:18:16.970: W/System.err(1754): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-09 03:18:16.970: W/System.err(1754): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-09 03:18:16.980: W/System.err(1754): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 03:18:16.990: W/System.err(1754): at android.os.Looper.loop(Looper.java:137)
07-09 03:18:16.990: W/System.err(1754): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-09 03:18:16.990: W/System.err(1754): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 03:18:17.010: W/System.err(1754): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 03:18:17.061: W/System.err(1754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-09 03:18:17.061: W/System.err(1754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-09 03:18:17.061: W/System.err(1754): at dalvik.system.NativeStart.main(Native Method)
07-09 03:18:17.090: W/System.err(1754): Caused by: java.lang.ExceptionInInitializerError
07-09 03:18:17.090: W/System.err(1754): at org.jsoup.nodes.Document$OutputSettings.<init>(Document.java:210)
07-09 03:18:17.150: W/dalvikvm(1754): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
07-09 03:18:17.150: W/System.err(1754): at org.jsoup.nodes.Document.<init>(Document.java:18)
07-09 03:18:17.160: W/System.err(1754): at org.jsoup.parser.TreeBuilder.initialiseParse(TreeBuilder.java:27)
07-09 03:18:17.160: W/System.err(1754): at org.jsoup.parser.TreeBuilder.parse(TreeBuilder.java:40)
07-09 03:18:17.160: W/System.err(1754): at org.jsoup.parser.HtmlTreeBuilder.parse(HtmlTreeBuilder.java:37)
07-09 03:18:17.170: W/System.err(1754): at org.jsoup.parser.Parser.parseInput(Parser.java:30)
07-09 03:18:17.170: W/System.err(1754): at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:102)
07-09 03:18:17.170: W/System.err(1754): at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:498)
07-09 03:18:17.215: W/System.err(1754): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:154)
07-09 03:18:17.215: W/System.err(1754): at com.example.weatherresponse.Weather$Retrieve_Weather.doInBackground(Weather.java:85)
07-09 03:18:17.220: W/System.err(1754): at com.example.weatherresponse.Weather$Retrieve_Weather.doInBackground(Weather.java:1)
07-09 03:18:17.220: W/System.err(1754): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-09 03:18:17.230: W/System.err(1754): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-09 03:18:17.230: W/System.err(1754): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-09 03:18:17.240: W/System.err(1754): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-09 03:18:17.240: W/System.err(1754): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-09 03:18:17.240: W/System.err(1754): at java.lang.Thread.run(Thread.java:856)
07-09 03:18:17.260: W/System.err(1754): Caused by: java.lang.ExceptionInInitializerError
07-09 03:18:17.290: W/System.err(1754): at org.jsoup.nodes.Entities$EscapeMode.<clinit>(Entities.java:20)
07-09 03:18:17.290: W/System.err(1754): ... 17 more
07-09 03:18:17.290: W/System.err(1754): Caused by: java.lang.NullPointerException: in == null
07-09 03:18:17.290: W/System.err(1754): at java.util.Properties.load(Properties.java:246)
07-09 03:18:17.290: W/System.err(1754): at org.jsoup.nodes.Entities.loadEntities(Entities.java:151)
07-09 03:18:17.290: W/System.err(1754): at org.jsoup.nodes.Entities.<clinit>(Entities.java:135)
07-09 03:18:17.290: W/System.err(1754): ... 18 more
07-09 03:18:17.383: I/System.out(1754): ExecExceptjava.lang.ExceptionInInitializerError
07-09 03:18:19.570: E/AndroidRuntime(1754): FATAL EXCEPTION: AsyncTask #1
07-09 03:18:19.570: E/AndroidRuntime(1754): java.lang.RuntimeException: An error occured while executing doInBackground()
07-09 03:18:19.570: E/AndroidRuntime(1754): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-09 03:18:19.570: E/AndroidRuntime(1754): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.lang.Thread.run(Thread.java:856)
07-09 03:18:19.570: E/AndroidRuntime(1754): Caused by: java.lang.ExceptionInInitializerError
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Document$OutputSettings.<init>(Document.java:210)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Document.<init>(Document.java:18)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.parser.TreeBuilder.initialiseParse(TreeBuilder.java:27)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.parser.TreeBuilder.parse(TreeBuilder.java:40)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.parser.HtmlTreeBuilder.parse(HtmlTreeBuilder.java:37)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.parser.Parser.parseInput(Parser.java:30)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:102)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:498)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:154)
07-09 03:18:19.570: E/AndroidRuntime(1754): at com.example.weatherresponse.Weather$Retrieve_Weather.doInBackground(Weather.java:85)
07-09 03:18:19.570: E/AndroidRuntime(1754): at com.example.weatherresponse.Weather$Retrieve_Weather.doInBackground(Weather.java:1)
07-09 03:18:19.570: E/AndroidRuntime(1754): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-09 03:18:19.570: E/AndroidRuntime(1754): ... 4 more
07-09 03:18:19.570: E/AndroidRuntime(1754): Caused by: java.lang.ExceptionInInitializerError
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Entities$EscapeMode.<clinit>(Entities.java:20)
07-09 03:18:19.570: E/AndroidRuntime(1754): ... 17 more
07-09 03:18:19.570: E/AndroidRuntime(1754): Caused by: java.lang.NullPointerException: in == null
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.Properties.load(Properties.java:246)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Entities.loadEntities(Entities.java:151)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Entities.<clinit>(Entities.java:135)
07-09 03:18:19.570: E/AndroidRuntime(1754): ... 18 more
我的代码正常运行,除了这条单一的基本行。我已经在模拟器和Android手机上测试了我的代码,并且都可以访问互联网。我尝试了多个网站,以及这些网站的移动版本,但都没有。我的清单和我的库文件在正确的位置有互联网权限。我在这个网站和其他人看了很多,以解决我的问题。由于我在相关帖子中找到的内容,我修改了我的代码和权限,但我还没有找到适合我的回答帖子。但是,我发现了一些未回答的帖子,提出了完全相同的问题。我发现我没有尝试过的唯一答案就是这个问题Error when releasing APK with AsyncTask and Jsoup:我无法弄清楚要改变哪个文件以测试它是否解决了我的问题。我对编程很新 - 任何帮助都会非常感激。谢谢!
答案 0 :(得分:7)
将这些行添加到proguard-project.txt
-keep public enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
答案 1 :(得分:1)
我的代码现在有效!我最终将我的.jar文件移动到lib中,而不是像Android-SDK r17 ruins working projects中所建议的那样将其用作引用的外部jar。我还添加了一个用户代理并重新编写了我的AsyncTask,以便Jsoup.connect( url )在onPreExecute中,同时在doInBackground中保留.get()。
private class Retrieve_Weather extends AsyncTask<Void, Void, String> {
private final String TAG = "Retrieve_Weather";
Connection con = null;
protected Void doInBackground(String... params) {
//Log.i(TAG, weatherline);
Document doc = null;
if (con !=null){
try {
Log.i(TAG, "trying document...");
doc = con.get();
Log.i(TAG, "after document");
Element currentWeather = doc.getElementById("curCond");
currentweather = currentWeather.text();
Log.i(TAG, currentWeather.text());
}
catch (IOException except) {
Log.i(TAG, "Cannot Connect", except);
except.printStackTrace();
currentweather = "Can't Connect";
Log.i(TAG, "End stacktrace");
}
}
else{
currentweather = "Connection returned null";
}
//Log.i(TAG, weatherline);
return currentweather;
}
@Override
protected void onPreExecute() {
//Log.i(TAG, "onPreExecute");
Log.i(TAG, "trying to connect...");
con = Jsoup.connect("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=zmw:02142.1.99999").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
Log.i(TAG, "CONNECTED!!!");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
@Override
protected void onPostExecute(Void result){
Log.i(TAG, "onPostExecute");
}
}
答案 2 :(得分:0)
查看org.jsoup.nodes.Entties https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/nodes/Entities.java的源代码
看起来像是试图从类路径加载“entities-base.properties”而无法找到它。您能否确保这些文件是“entities-full.properties”可用且在类路径中。
只是一个未来调试的提示总是寻找最深的异常,在你的情况下
07-09 03:18:17.290: W/System.err(1754): Caused by: java.lang.NullPointerException: in == null
07-09 03:18:17.290: W/System.err(1754): at java.util.Properties.load(Properties.java:246)
07-09 03:18:17.290: W/System.err(1754): at org.jsoup.nodes.Entities.loadEntities(Entities.java:151)
07-09 03:18:17.290: W/System.err(1754): at org.jsoup.nodes.Entities.<clinit>(Entities.java:135)
最后一行指出了我要去的地方。希望这有帮助
答案 3 :(得分:0)
经过一番研究,我相信这实际上是一个Android漏洞。似乎Android缓存META-INF/MANIFEST.MF
文件,但在重新安装应用程序时不会使缓存失效。因此,系统无法看到新的.property
文件,导致getResourceAsStream
返回null
。
解决方案:重启设备。