0

I need to get values from internet using AsyncTask and JSoup, I want to convert the scraped data from JSoup an make them into a String I can use later on my class to build a List. I was wondering if I can create a String from the JSoup result inside the AsyncTask and use it on an external class? I was thinking on something like this:

String url = "http://mysite.com.data.php";
public class Title extends AsyncTask<Void, Void, Void> {
    String title;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to the web site
            Document document = Jsoup.connect(url).get();
            // Get the html document title
            title = document.title();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        String myString = title; //<---TAKE THIS STRING FROM HERE
    }//                                       |
}//                                           V
String my2ndString = myString; //<---------AND USE IT HERE

I am java noob, can you please help me

Valerie Castle
  • 113
  • 1
  • 11
  • oh I edited the URL on my question, the data I am getting is not an json it will be a scraped data from another website. – Valerie Castle Nov 29 '16 at 21:48
  • Declare `myString` globally or use an `interface`. See here http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a – K Neeraj Lal Nov 30 '16 at 03:51
  • I tried declaring a global variable `public static String TITLE_CONTENT;` but I still can't get it outside to use it, [see example here](https://github.com/valerie16901/examples/edit/master/MovieList.java) – Valerie Castle Dec 02 '16 at 11:04
  • You can only use `setupMovies` only after the request has completed, as `AsyncTask` is Asynchronous. – K Neeraj Lal Dec 03 '16 at 15:17
  • Have you worked it out yet? – K Neeraj Lal Dec 05 '16 at 15:35

0 Answers0