Search code examples
androidjsoup

Allow user to change JSOUP URL


My goal is to give the user the ability to change the end of a URL based on information entered by the user.

Here is the code I have for the JSOUP.

private void getWebsite() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            final StringBuilder builder = new StringBuilder();
            final StringBuilder builder2 = new StringBuilder();
            final StringBuilder builder3 = new StringBuilder();

            try {
                Document doc = Jsoup.connect("www.randomurl.com").get();
                Elements links = doc.select("div1");
                Elements links2 = doc.select("div2");
                Elements links3 = doc.select("div3");

                for (Element link : links) {
                    builder.append("\n").append(link.text());
                    builder2.append("\n").append(links2.text());
                    builder3.append("\n").append(links3.text());

                }
            } catch (IOException e) {
                builder.append("Error : ").append(e.getMessage()).append("\n");
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    result.setText(builder.toString());
                    worth.setText(builder2.toString());
                    price.setText(builder3.toString());
                }
            });
        }
    }).start();
}

My goal is to have a box where a user can enter in information and this is applied to the last part of the URL. For example like this.

try {
     Document doc = Jsoup.connect("www.randomurl.com/" + *userEnteredStringHere*).get();

Any insight on how to complete this would be awesome!

EDIT :-

@Override
public void run() {
    final StringBuilder builder = new StringBuilder();
    final StringBuilder builder2 = new StringBuilder();
    final StringBuilder builder3 = new StringBuilder();
    EditText userInput = findViewById(R.id.userInputWidget);

    try {
        Document doc = Jsoup.connect("www.randomurl.com" + userInput.getText()).get();

Solution

  • It's not really a Jsoup question. I bet your app has some GUI with layout defined as xml file. It should contain EditText widget so user will be able to input text:

    <EditText
        android:id="@+id/userInputWidget"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:inputType="text"/>
    

    Then you can access it in your code:

     EditText userInput = (EditText) findViewById(R.id.userInputWidget);
     userInput.getText();