0

The urls are just text files like www.example.com/example.txt so what I need to do is get the whole text from the website. The text can be very long up to 1MB. I dont know how I should modify my code to do this.

Here is my code

private class Title extends AsyncTask<Void, Void, Void> {
    String text;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(Story.this);
        progressDialog.setMessage("Loading");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Document document = Jsoup.connect(url).get();              
            text = document.text();    //I made this part up. Definitely WRONG
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        story.add(text);        //story is an array
        progressDialog.dismiss();
    }
}

I call it with new Title().execute();

This code doesn't work I cant get the text. Nothing happens.

Murat Kaya
  • 193
  • 1
  • 18
  • just wondering, why are you using Jsoup and Document class if you're reading a text file? Try this? http://stackoverflow.com/questions/2922210/reading-text-file-from-server-on-android – Steve M Nov 28 '13 at 22:20
  • doInBackground method returns null and so what will you use on onPostExecute? I think doInBackground must return sth to use onPostExecute. – Batuhan Coşkun Nov 28 '13 at 22:27
  • note however that answer doesn't seem complete with regards to making sure the stream is closed, and doesn't set a timeout on the request, so buyer beware. – Steve M Nov 28 '13 at 22:27

1 Answers1

0

What you should be doing is adding all of the Elements to an Elements object. See below:

@Override
protected Void doInBackground(Void... params) {
    try {
        Document document = Jsoup.connect(url).get();              
    } catch (IOException e) {
        e.printStackTrace();
    }
    Elements elem = null; 
    elem = document.select("*");
    Log.i("Value of elem", String.valueOf(elem);
    return null;
}

Then for your onPostExecute:

@Override
protected void onPostExecute(Void result) {
    String valueofelement = elem.text();
    story.add(valueofelement);        //story is an array
    progressDialog.dismiss();
   }
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
  • I am getting a nullPointerExeption here... for (Element element : elem) – Murat Kaya Nov 29 '13 at 00:43
  • I fixed the NPE but now the text is still not loading although I am not getting any errors. Here is my updated code http://pastebin.com/0H0971m6 – Murat Kaya Nov 29 '13 at 14:57
  • Try changing it to the iterator, instead of using the `for` loop. – hichris123 Nov 29 '13 at 15:18
  • Here is the code with iterator http://pastebin.com/xTgz3eWp Text is still not loaded. Also since the array story populates my listview wouldn't it thousands of listview items? I want the whole text to be added as one element thus having one listitem. – Murat Kaya Nov 29 '13 at 15:32
  • Oh, that makes sense. Try this updated code. Also, check with the log statement that I put in that `elem` actually contains text. – hichris123 Nov 29 '13 at 15:44
  • I can see the text that is supposed to be loaded in the Log but it still doesn't load in the app. – Murat Kaya Nov 29 '13 at 16:05
  • Log `story` to make sure that it contains the text. – hichris123 Nov 29 '13 at 16:12
  • Yes story contains the text. The problem seems to be that the listview is not populating. – Murat Kaya Nov 29 '13 at 16:25
  • Post another question with your ListView. The problem is not with `story`, it is with the ListView code, and as such this problem is solved. – hichris123 Nov 29 '13 at 16:28