Note that there are some explanatory texts on larger screens.

plurals
  1. POAsyncTask in Android?
    primarykey
    data
    text
    <p>I am working on parsing a website in my application.</p> <p>This is the website: <a href="https://raw.github.com/currencybot/open-exchange-rates/master/latest.json" rel="nofollow">https://raw.github.com/currencybot/open-exchange-rates/master/latest.json</a></p> <p>Since I have had some trouble with this, I first tried to get the entire website using a class that extends AsyncTask.</p> <p>Here is the code which gets EVERYTHING from the website above:</p> <pre><code> private class DownloadWebPageTask extends AsyncTask&lt;String, Void, String&gt; { @Override protected String doInBackground(String... urls) { String response = ""; for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse execute = client.execute(httpGet); InputStream content = execute.getEntity().getContent(); BufferedReader buffer = new BufferedReader( new InputStreamReader(content)); String s = ""; while ((s = buffer.readLine()) != null) { response += s; } } catch (Exception e) { e.printStackTrace(); } } return response; } } </code></pre> <p>However, if you go to the website, I only want the portion after rates. I have to edit the above code so it only returns what I need.</p> <p>Based on what I understand, I know that I need to change String response to an ArrayList, and I need to change the while loop, but I don't know what I should do exactly.</p> <p>I tried using the following code to change the method, but it did NOT work.</p> <pre><code> private class DownloadWebPageTask extends AsyncTask&lt;String, Void, ArrayList&lt;String&gt;&gt; { @Override protected ArrayList&lt;String&gt; doInBackground(String... urls) { ArrayList&lt;String&gt; response = new ArrayList&lt;String&gt;(); for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse execute = client.execute(httpGet); InputStream content = execute.getEntity().getContent(); BufferedReader buffer = new BufferedReader( new InputStreamReader(content)); String s = response.toString(); while ((s = buffer.readLine()) != null) { JSONArray ja = new JSONArray(s); for (int i = 0; i &lt; ja.length(); i++) { JSONObject jo = (JSONObject) ja.get(i); response.add(jo.getString("rates")); } } } catch (Exception e) { e.printStackTrace(); } } return response; } } </code></pre> <p>Any help would be greatly appreciated.</p> <p>@Dheeraj, UPDATE:</p> <pre><code> while ((s = buffer.readLine()) != null) { JSONArray ja = new JSONObject().getJSONArray("rates"); response.add(ja.toString()); } </code></pre> <p>This is what I have so far Dheeraj. I am stuck on how to provide the buffer to a JSONObject. Can you help me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload