Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating a ListView w/ AsyncTask
    primarykey
    data
    text
    <p>This is probably not very elegant, but what I'm trying to do is connect to a web service, fetch the JSON, parse it, create an object out of it, add that object to an ArrayList and then use that ArrayList to populate my ListView. </p> <p>I'm trying to do all of this with AsyncTask.</p> <p>SUMMARY: doInBackgroud takes a String of a url, uses it to connect to a web service. I get the JSON data as a string, parse it, construct a new object out of the data, and add it to ArrayList. Then in <code>onPostExecute</code> I'm trying to set the listadapter using an <code>ArrayAdapter</code> that utilizes my ArrayList.</p> <p>Here's what I have:</p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import org.json.JSONArray; import org.json.JSONObject; import oauth.signpost.OAuthConsumer; import oauth.signpost.basic.DefaultOAuthConsumer; import android.app.ListActivity; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.widget.ArrayAdapter; public class AllOffersListActivity extends ListActivity { private static final String CONSUMER_KEY = "bla"; private static final String CONSUMER_SECRET = "bla"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new CreateArrayListTask().execute("http://example.com/sample.json"); } private class CreateArrayListTask extends AsyncTask&lt;String, Void, ArrayList&lt;Offer&gt;&gt; { private final ProgressDialog dialog = new ProgressDialog(AllOffersListActivity.this); @Override protected void onPreExecute() { this.dialog.setMessage("Fetching offers..."); this.dialog.show(); } @Override protected ArrayList&lt;Offer&gt; doInBackGround(String...urls) { ArrayList&lt;Offer&gt; offerList = new ArrayList&lt;Offer&gt;(); for(String url: urls) { OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); consumer.setTokenWithSecret("", ""); try { URL url1 = new URL(url); HttpURLConnection request = (HttpURLConnection) url1.openConnection(); // sign the request consumer.sign(request); // send the request request.connect(); String JSONString = convertStreamToString(request.getInputStream()); JSONObject jObject = new JSONObject(JSONString); JSONObject offerObject = jObject.getJSONObject("offer"); String titleValue = offerObject.getString("title"); //System.out.println(titleValue); String descriptionValue = offerObject.getString("description"); //System.out.println(attributeValue); JSONObject businessObject = offerObject.getJSONObject("business"); String nameValue = businessObject.getString("name"); Offer myOffer = new Offer(titleValue, descriptionValue, nameValue); offerList.add(myOffer); } catch (Exception e) { e.printStackTrace(); } } return offerList; } @Override protected void onPostExecute(ArrayList&lt;Offer&gt; offerList) { if(this.dialog.isShowing()) this.dialog.dismiss(); setListAdapter(new ArrayAdapter&lt;Offer&gt;(AllOffersListActivity.this, android.R.layout.simple_list_item_1, offerList)); } } private String convertStreamToString(InputStream inputStream) throws IOException { if(inputStream != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader( new InputStreamReader(inputStream, "UTF-8")); int n; while((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { inputStream.close(); } return writer.toString(); } else { return ""; } } } </code></pre> <p>I'm seeing two errors. One is on my private Async class: <code>"The type AllOffersListActivity.CreateArrayListTask must implement the inherited abstract method AsyncTask&lt;String,Void,ArrayList&lt;Offer&gt;&gt;.doInBackground(String...)"</code></p> <p>Secondly, on my <code>doInBackGround</code> Override, I'm getting: <code>The method doInBackGround(String...) of type AllOffersListActivity.CreateArrayListTask must override or implement a supertype method</code></p> <p>What am I missing here?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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