1

Theres a "Method getText Must Be Called from the UI Thread" error under

edtUrl.getText().toString();

I Think i have to add a constructor to AsyncTask which would take the two Strings then send them when creating your task? Anyone have any ideas? Tried using another SO question just like this info but nothing worked

 private class ParseURL extends AsyncTask<String, Void, String> {
      private String siteUrl;
    @Override
    protected String doInBackground(String... strings) {
        StringBuffer buffer = new StringBuffer();
        //EditText edtUrl = (EditText) findViewById(R.id.edtURL);

        //String siteUrl = edtUrl.getText().toString();
        try {
            Log.d("JSwa", "Connecting to ["+strings[0]+"]");
            Document doc  = Jsoup.connect(strings[0]).get();
            Log.d("JSwa", "Connected to ["+strings[0]+"]");
            // Get document (HTML page) title
            String title = doc.title();
            Log.d("JSwA", "Title ["+title+"]");
            buffer.append("Title: " + title + "\r\n");


            try {
                doc = Jsoup.connect(siteUrl)
                        .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
                        .referrer("http://www.google.com")
                        .timeout(1000 * 5) //it's in milliseconds, so this means 5 seconds.
                        .get();
            } catch (Throwable t) {
                t.printStackTrace();
            }

            Elements tableElements = doc.select("td");
            for (Element td : tableElements) {
                buffer.append("TT [" + td + "] \r\n");
                Log.d("JSwA", "TT [" + td + "]");
            }

This is my onClick(), Im getting errors under both the siteURl below.

       @Override
        public void onClick(View view) {
           // String siteUrl = edtUrl.getText().toString();
            siteUrl = "http://www.w3schools.com/html/html_tables.asp";
            (new ParseURL()).execute(new String[]{siteUrl});

This is my findViewsById() method i was trying to use.

          protected void onPreExecute() {
        super.onPreExecute();
        EditText edtUrl = (EditText) findViewById(R.id.edtURL);
         siteUrl = edtUrl.getText().toString();
    }

The error appear while trying to incorporate this code into my project, It is part of my parseURL method:

 try {
        doc = Jsoup.connect(siteUrl)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
                .referrer("http://www.google.com")
                .timeout(1000 * 5) //it's in milliseconds, so this means 5 seconds.
                .get();
    } catch (Throwable t) {
        t.printStackTrace();
    }
DylanB
  • 23
  • 3

2 Answers2

3

Only the UI thread can work with the UI components. doInbackground() runs on a different Thread which can't access the UI components.

You can set the string as modular and use the onPreExecute() method to get the text and then use it in the doInBackground() method:

private class ParseURL extends AsyncTask<String, Void, String> {
    private String siteUrl;

    .
    .
    .


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        EditText edtUrl = (EditText) findViewById(R.id.edtURL);
        siteUrl = edtUrl.getText().toString();
    }


@Override
protected String doInBackground(String... strings) {
    try {
        doc = Jsoup.connect(siteUrl)
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
            .referrer("http://www.google.com")
            .timeout(1000 * 5) //it's in milliseconds, so this means 5 seconds.
            .get();
    } catch (Throwable t) {
        t.printStackTrace();
    }

    .
    .
    .

}

Hope it helps.

Etienne GT
  • 185
  • 8
  • I did what you did but getting an error now, i edited my answer above if you want to have a look – DylanB Apr 12 '16 at 18:00
0

It looks like you are trying to get the url that the user entered, and then contact a server to do something with it. The way to do this is to send the url in when you create your task, and then retrieve it as an argument to your doInBackground() method within the Task.

So you would pass in the site url with this statement: (new ParseURL()).execute(siteUrl);

And then you would retrieve it in your doInBackground() method like this:

@Override
protected String doInBackground(String... strings) {
    String siteUrl = strings[0];
}

The line (String... strings) allows you to send in any number of String arguments, and a dynamic array will be created with whatever parameters you pass in. So since we only passed in one String (our site url), a String array with size 1 is created with our url as the only parameter. So now you just have to access it like above.

The reason you are getting the "UI Thread" error is because you cannot manipulate UI elements off the UI thread. And since an AsyncTask runs OFF the UI thread in the background, you receive the error. The solution is to get the data you need while still on the UI thread, then just pass it into the Task when you create it.

Hope this clarifies things for you!

NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29