I'm having trouble running my Android application with Jsoup. It is a simple example, but it is returning nothing.
Java class:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView)findViewById(R.id.textView1);
Document doc = null;
try {
doc = Jsoup.connect("http://google.com").get();
String title = doc.title();
textview.setText(title);
} catch (IOException e) {
textview.setText("aaaa");
}
}
}
Layout activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="68dp"
android:layout_marginTop="63dp" />
</RelativeLayout>
manifest.xml:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.persmission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.teste12.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Note: It was also inserted in the path of jsoup.jar.
Follow the log below:
03-09 00:53:39.281: E/AndroidRuntime(8413): FATAL EXCEPTION: main
03-09 00:53:39.281: E/AndroidRuntime(8413): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.study01/com.example.study01.MainActivity}: android.os.NetworkOnMainThreadException
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2178)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.app.ActivityThread.access$700(ActivityThread.java:141)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.os.Looper.loop(Looper.java:137)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.app.ActivityThread.main(ActivityThread.java:5118)
03-09 00:53:39.281: E/AndroidRuntime(8413): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 00:53:39.281: E/AndroidRuntime(8413): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 00:53:39.281: E/AndroidRuntime(8413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-09 00:53:39.281: E/AndroidRuntime(8413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-09 00:53:39.281: E/AndroidRuntime(8413): at dalvik.system.NativeStart.main(Native Method)
03-09 00:53:39.281: E/AndroidRuntime(8413): Caused by: android.os.NetworkOnMainThreadException
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)
03-09 00:53:39.281: E/AndroidRuntime(8413): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-09 00:53:39.281: E/AndroidRuntime(8413): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-09 00:53:39.281: E/AndroidRuntime(8413): at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
03-09 00:53:39.281: E/AndroidRuntime(8413): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
03-09 00:53:39.281: E/AndroidRuntime(8413): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:439)
03-09 00:53:39.281: E/AndroidRuntime(8413): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
03-09 00:53:39.281: E/AndroidRuntime(8413): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
03-09 00:53:39.281: E/AndroidRuntime(8413): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
03-09 00:53:39.281: E/AndroidRuntime(8413): at com.example.study01.MainActivity.onCreate(MainActivity.java:23)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.app.Activity.performCreate(Activity.java:5058)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-09 00:53:39.281: E/AndroidRuntime(8413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2104)
03-09 00:53:39.281: E/AndroidRuntime(8413): ... 11 more