I downloaded the jsoup library jsoup-1.7.1.jar core and imported it to my project using the Project -> Properties->Java Build Path -> Add external Jars and I pasted the library file to my libs folder. However there seems to be some problem about importing the Jsoup library to my project. When I run my app, upon launching I get this error.
12-26 22:59:24.133: E/AndroidRuntime(6710): FATAL EXCEPTION: main
12-26 22:59:24.133: E/AndroidRuntime(6710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsouptest/com.example.jsouptest.MainActivity}: android.os.NetworkOnMainThreadException
After searching and asking I found out that eclipse sees the jsoup.jar, but unable to package it to APK file for the app to run. I tried to organize Imports by pressing Shift+Alt+O and I would get the same error. At this point, I am unsure about what is wrong and have no idea how to fix it. I have only hope someone will lead me toward the solution. Appreciate your time!
This is my code:
package com.example.jsouptest;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Document doc;
try {
doc = Jsoup.connect("http://google.com/").get();
String title = doc.title();
System.out.print(title);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…