Search code examples
androidjsoupillegalstateexception

IllegalStateException upon executing Jsoup POST request


I'm sending a POST request using Jsoup and the app crashes upon executing said request.

I tried with AsyncTask and the same problem persists.

This is my code in the main activity:

Fetcher fetcher = new Fetcher();
        fetcher.doInBackground();

This is the code in AsynkTask:

class Fetcher extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            //Connect to the website
            Connection.Response response =
                    Jsoup.connect("https://parts-of-speech.info/tagger/tagger")
                            .userAgent("Mozilla/5.0")
                            .timeout(10 * 1000)
                            .method(Connection.Method.POST)
                            .data("text", "This is a text.")
                            .data("language", "en")
                            .followRedirects(true)
                            .ignoreContentType(true)
                            .execute();
            Document document = response.parse();
            System.out.println(document.text());

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

This is the expected response:

{
    "taggedText": "This_NE is_VAFIN a_ART text_ADJD ._$. "
}

This is the full stack trace for the error:


10/24 14:05:09: Launching app
$ adb install-multiple -r -t -p com.jwanhsulaiman.lingu C:\Users\Lenovo\Desktop\Lingu\app\build\intermediates\split-apk\debug\slices\slice_2.apk 
Split APKs installed in 1 s 624 ms
$ adb shell am start -n "com.jwanhsulaiman.lingu/com.jwanhsulaiman.lingu.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 2586 on device sony-g8141-CB512G32W7
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/AssistStructure: Flattened final assist data: 2648 bytes, containing 1 windows, 10 views
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
I/chatty: uid=10082(com.jwanhsulaiman.lingu) identical 2 lines
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.jwanhsulaiman.lingu, PID: 2586
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:6612)
        at android.view.View.performClickInternal(View.java:6581)
        at android.view.View.access$3100(View.java:785)
        at android.view.View$PerformClick.run(View.java:25904)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:280)
        at android.app.ActivityThread.main(ActivityThread.java:6706)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:6612) 
        at android.view.View.performClickInternal(View.java:6581) 
        at android.view.View.access$3100(View.java:785) 
        at android.view.View$PerformClick.run(View.java:25904) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:280) 
        at android.app.ActivityThread.main(ActivityThread.java:6706) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
     Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
        at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
        at java.net.InetAddress.getAllByName(InetAddress.java:1154)
        at com.android.okhttp.Dns$1.lookup(Dns.java:39)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
        at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:26)
        at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:730)
        at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:705)
        at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:295)
        at com.jwanhsulaiman.lingu.Fetcher.doInBackground(Fetcher.java:31)
        at com.jwanhsulaiman.lingu.MainActivity.buttonOnClick(MainActivity.java:62)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:6612) 
        at android.view.View.performClickInternal(View.java:6581) 
        at android.view.View.access$3100(View.java:785) 
        at android.view.View$PerformClick.run(View.java:25904) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:280) 
        at android.app.ActivityThread.main(ActivityThread.java:6706) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
I/Process: Sending signal. PID: 2586 SIG: 9
Application terminated.

What could be the problem?


Solution

  • I can't choose an answer from the comments so I'll post the answer here, thanks to @Kishan Maurya for pointing out that AsyncTask is a must, and to @Andy for instructing me to call the AsyncTask properly, by calling execute() not doInBackground

    This is how to call AsyncTask properly:

    AsyncTask post = new AsyncTask(); post.execute();

    The code for AsyncTask was okay.