所以,我的Html_parser.java文件中有以下行,这会导致运行时错误:
import java.io.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Html_parser {
public static String[] getdata(String site) throws IOException {
String[] data = new String [1];
for (int i = 0; i < data.length; i++){
data[i] = ""; //Initializing the array
}
try {
// Connect to the web site
Document doc = Jsoup.connect(site).get(); //CRASHES HERE
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}
错误的第一行: java.lang.RuntimeException:无法启动活动ComponentInfo {com.asd.mordorviewer / com.asd.mordorviewer.MainActivity}:android.os.NetworkOnMainThreadException
如果我发表评论&#34; CRASHES HERE&#34;行,然后应用程序运行没有错误。
我有文件root / libs / jsoup-1.7.3.jar,它是在java构建路径中导入的。
我不明白这里有什么问题。
EDIT1 :我已经从准备好的简短示例代码中完成了复制粘贴,虽然该代码在给定项目中正确运行,但在我的项目中会导致运行时错误。
EDIT2 :我使用的示例代码是针对API级别18,现在是:
public class MainActivity extends Activity {
// URL Address
String url = "http://www.stackoverflow.com";
ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Title().execute();
}
// Title AsyncTask
private class Title extends AsyncTask<Void, Void, Void> {
String title;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Get the html document title
title = document.title();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) findViewById(R.id.titletxt);
txttitle.setText(title);
mProgressDialog.dismiss();
}
}
}
EDIT3 :我重新启动了Eclipse并创建了一个新项目,试图复制源项目的每个属性,但仍然没有。
EDIT4
08-16 22:14:03.096: W/dalvikvm(28383): threadid=11: thread exiting with uncaught exception (group=0x415d18b0)
08-16 22:14:03.126: E/AndroidRuntime(28383): FATAL EXCEPTION: AsyncTask #1
08-16 22:14:03.126: E/AndroidRuntime(28383): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 22:14:03.126: E/AndroidRuntime(28383): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-16 22:14:03.126: E/AndroidRuntime(28383): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.lang.Thread.run(Thread.java:841)
08-16 22:14:03.126: E/AndroidRuntime(28383): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
08-16 22:14:03.126: E/AndroidRuntime(28383): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
08-16 22:14:03.126: E/AndroidRuntime(28383): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
08-16 22:14:03.126: E/AndroidRuntime(28383): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
08-16 22:14:03.126: E/AndroidRuntime(28383): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
08-16 22:14:03.126: E/AndroidRuntime(28383): at com.asd.mordorviewer.MainActivity$Title.doInBackground(MainActivity.java:50)
08-16 22:14:03.126: E/AndroidRuntime(28383): at com.asd.mordorviewer.MainActivity$Title.doInBackground(MainActivity.java:1)
08-16 22:14:03.126: E/AndroidRuntime(28383): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-16 22:14:03.126: E/AndroidRuntime(28383): ... 4 more
08-16 22:14:03.126: E/AndroidRuntime(28383): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.io.Posix.getaddrinfo(Native Method)
08-16 22:14:03.126: E/AndroidRuntime(28383): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
08-16 22:14:03.126: E/AndroidRuntime(28383): at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
08-16 22:14:03.126: E/AndroidRuntime(28383): ... 24 more
08-16 22:14:03.126: E/AndroidRuntime(28383): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
08-16 22:14:03.126: E/AndroidRuntime(28383): ... 27 more
08-16 22:14:03.416: E/WindowManager(28383): Activity com.asd.mordorviewer.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{418ba2e8 V.E..... R....... 0,0-684,324} that was originally added here
08-16 22:14:03.416: E/WindowManager(28383): android.view.WindowLeaked: Activity com.asd.mordorviewer.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{418ba2e8 V.E..... R....... 0,0-684,324} that was originally added here
08-16 22:14:03.416: E/WindowManager(28383): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:350)
08-16 22:14:03.416: E/WindowManager(28383): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
08-16 22:14:03.416: E/WindowManager(28383): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
08-16 22:14:03.416: E/WindowManager(28383): at android.app.Dialog.show(Dialog.java:289)
08-16 22:14:03.416: E/WindowManager(28383): at com.asd.mordorviewer.MainActivity$Title.onPreExecute(MainActivity.java:43)
08-16 22:14:03.416: E/WindowManager(28383): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
08-16 22:14:03.416: E/WindowManager(28383): at android.os.AsyncTask.execute(AsyncTask.java:534)
08-16 22:14:03.416: E/WindowManager(28383): at com.asd.mordorviewer.MainActivity.onCreate(MainActivity.java:29)
08-16 22:14:03.416: E/WindowManager(28383): at android.app.Activity.performCreate(Activity.java:5133)
08-16 22:14:03.416: E/WindowManager(28383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-16 22:14:03.416: E/WindowManager(28383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
08-16 22:14:03.416: E/WindowManager(28383): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
08-16 22:14:03.416: E/WindowManager(28383): at android.app.ActivityThread.access$600(ActivityThread.java:150)
08-16 22:14:03.416: E/WindowManager(28383): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
08-16 22:14:03.416: E/WindowManager(28383): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 22:14:03.416: E/WindowManager(28383): at android.os.Looper.loop(Looper.java:213)
08-16 22:14:03.416: E/WindowManager(28383): at android.app.ActivityThread.main(ActivityThread.java:5225)
08-16 22:14:03.416: E/WindowManager(28383): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 22:14:03.416: E/WindowManager(28383): at java.lang.reflect.Method.invoke(Method.java:525)
08-16 22:14:03.416: E/WindowManager(28383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
08-16 22:14:03.416: E/WindowManager(28383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-16 22:14:03.416: E/WindowManager(28383): at dalvik.system.NativeStart.main(Native Method)
错误输出。
答案 0 :(得分:1)
在AndroidManifest.xml文件中,必须添加以下行:
<uses-permission android:name="android.permission.INTERNET" />
似乎我在jsoup设置中看得太多而忘记了显而易见的事情。
答案 1 :(得分:0)
问题是您正在尝试在主线程上执行网络操作 - 这在API 11及更高版本中是不允许的。
当应用程序尝试在其主线程上执行网络操作时引发的异常。
您提到的另一个项目可能针对的是低于11的API版本。
在后台任务中进行网络处理,例如AsyncTask
。