简体   繁体   中英

Jsoup connection problems with Android application

So I am using Jsoup for my project. The backend code is working fine on the output menu but when I am trying to put it on the android studio, it is not working. I imported the libraries perfectly and no error in the code. I also gave access to the internet using

<uses-permission android:name="android.permission.INTERNET" />

So I thought of making a new mini-project. So now my code looks like

'''
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class MainActivity extends AppCompatActivity {

    public Button button;
    public TextView textview;
    public EditText edittext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
        edittext = (EditText) findViewById(R.id.editText);

        View.OnClickListener search = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Document doc = Jsoup.connect("https://www.flipkart.com").get();
                    String title = doc.text();
                    textview.append(title);

                } catch (Exception e){

                }
            }
        };
        button.setOnClickListener(search);
    }
} '''

But nothing is printing in the text view. Please tell me the mistake.

Can you try this:

Executor mExecutor  = Executors.newSingleThreadExecutor();

Tasks.call(mExecutor, new Callable<String>() {
     @Override
     public String call() throws Exception {
         Document doc = Jsoup.connect("https://www.flipkart.com").get();
         String title = doc.text();
         return title;
     }
}).continueWith(new Continuation<String, Object>() {

     @Override
     public Object then(@NonNull Task<String> task) throws Exception {
         textview.append( task.getResult());
         return null;
     }
});

Using Executors.newSingleThreadExecutor() on the other hand allows you to submit multiple tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM