Reputation: 324
So, I have the following lines in my Html_parser.java file which causes a runtime error:
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;
}
}
First line of the errors: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asd.mordorviewer/com.asd.mordorviewer.MainActivity}: android.os.NetworkOnMainThreadException
If I comment out the "CRASHES HERE" line, then the application runs without errors.
I have the file root/libs/jsoup-1.7.3.jar and it is imported in the java build path.
I don't understand what is wrong here.
EDIT1: I've done a copy-paste from a ready short example code and although that code runs correctly in the given project, in my project causes runtime error.
EDIT2: The example code I used is targeting API level 18, and here it is:
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: I restarted Eclipse and created a new project trying to copy each attribute of the source project, still nothing.
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)
The error output.
Upvotes: 0
Views: 434
Reputation: 324
In the AndroidManifest.xml file the following line had to be added:
<uses-permission android:name="android.permission.INTERNET" />
Seems I was looking too much in the jsoup setup and forgot the obvious.
Upvotes: 1
Reputation: 4708
The problem is that you're trying to perform a network operation on the main thread - this is not allowed in API 11 and above.
From the documentation:
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
The other project you mention probably targets an API version lower than 11.
Do your network processing in a background task, e.g. AsyncTask
.
Upvotes: 0