0

I have an android application and using AsyncTask I want to download a JSON file from a website.

So far I have

public class DownloaderTask extends AsyncTask<String, Void, String[]> {

private MyActivity myactivity;
private Context context;
private String rawFeed[] = new String[3];

DownloaderTask(MyActivity parent) {

    myactivity = parent;
    context = parent.getApplicationContext();
}

@Override
protected String[] doInBackground(String... params) {

    boolean complete = false;
    InputStream input = null;
    OutputStream output = null;
    HttpURLConnection connection = null;

    for (int i = 0; i < params.length; i++) {

        try {
            URL url = new URL(params[i]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            input = connection.getInputStream();
        }
        catch (Exception e) {

        }
    }

    return rawFeed;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onPostExecute(String[] strings) {

    if (myactivity != null) {

        myactivity.setRefreshed(strings);
    }
}

Im not sure how to continue from here

The website I'm downloading from is : https://d396qusza40orc.cloudfront.net/android%2FLabs%2FUserNotifications%2Ftaylorswift.txt

and when you go to the site it's just a page with a bunch of text on it, a JSON file.

The parameters that get passed into the AsyncTask is an array of 3 strings, each string containing the URL that I need to download from

  • AsyncTasks have issues, mainly around the lifecycle of an App. Have you tried using the library Volley for this? You could even use GSON (if you need deserialising) inside a subclassed Volley class. See this SO post http://stackoverflow.com/questions/24537875/making-a-gson-request-using-volley – Graham Smith Nov 04 '14 at 16:26
  • I want to use AsyncTask to get familiar with function of it. What kind of issues? Are they so bad that people generally don't use it? @GrahamSmith – masterCoder Nov 04 '14 at 16:31

1 Answers1

1

To read all the contents of an input stream I use:

String inputStreamToString(InputStream is)
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder stringBuilder = new StringBuilder();
    String line = null;
    try
    {
        while ((line = reader.readLine()) != null)
        {
            stringBuilder.append(line);
        }
        is.close();
    }
    catch (IOException e)
    { }
    return stringBuilder.toString();
}

Then, to parse it as a JSON you just can user the Java JSONObject constructing it with that string.

JML
  • 611
  • 3
  • 9
  • so getting an InputStream, the following code would read the contents? – masterCoder Nov 04 '14 at 16:35
  • if the content is text, yes (not sure what is the behaviour of this method if trying to read something else, preobably it'll work and generate a nonsense string) – JML Nov 04 '14 at 16:39
  • what if it's in a for loop? Where would you place the is.close()? – masterCoder Nov 04 '14 at 16:41
  • in your case, inside each interation of the loop, as you are opoening a new connection and getting a new InputStream in each iteration. Normally, you should add a finally statement after the catch, and (if the input isn't null) close it there, to asure that it's closed not matter the result of the try-catch – JML Nov 04 '14 at 16:44
  • pretty sure my asynctask returned a null string array...going to look into it in more depth in a bit – masterCoder Nov 04 '14 at 16:51
  • i forgot to assign the reader to my string, after that it worked! – masterCoder Nov 04 '14 at 19:05