java – Why Jsoup wont connect and always get exception?-ThrowExceptions

Exception or error:
try {
        Document doc = Jsoup.connect("https://en.wikipedia.org/").get();

    } catch (Exception e) {
        e.printStackTrace();
    }

This simple code just won’t work, i always get exception and don’t know why, i add implementation ‘org.jsoup:jsoup:1.12.2’ in gradle and internet permission in Manifest, and I not sure why i cant get document.

02-21 17:55:40.569 9999-9999/com.example.myapplication E/Some Tag: null
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    at java.net.InetAddress.getAllByName(InetAddress.java:214)
    at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
    at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
    at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
    at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:161)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:732)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:707)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:297)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:286)
    at com.example.myapplication.VestActivity.onCreate(VestActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5019)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
How to solve:

There are a lot of solutions to avoid android.os.NetworkOnMainThreadException the easiest one could be to create a thread as below

new Thread(new Runnable(){
@Override
public void run() {
    Document doc = Jsoup.connect("https://en.wikipedia.org/").get();
}}).start();

If you are using rxJava another approach could be

    Completable.fromCallable(() -> {
                  Document doc = Jsoup.connect("https://en.wikipedia.org/").get();
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .unsubscribeOn(Schedulers.io());

Another approach could be use asynctask as below

new AsyncTask<Void, Void, Void>(){
public void doInBackground(Void... params){
         Document doc = Jsoup.connect("https://en.wikipedia.org/").get();

}}.execute();

###

Run it in AsyncTask or in a different thread. You’re using the UI thread which throws an exception when you’re trying to perform a networking operation on it.

Alternatively disable strict mode for debugging only:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy);

Leave a Reply

Your email address will not be published. Required fields are marked *