填充ListView w / AsyncTask

这可能不是很优雅,但我想要做的是连接到一个Web服务,获取JSON,parsing它,从中创build一个对象,将该对象添加到ArrayList,然后使用该ArrayList填充我的列表显示。

我正在尝试使用AsyncTask来完成所有这一切。

摘要:doInBackgroud接受一个URL的string,用它来连接到一个Web服务。 我将JSON数据作为string获取,parsing,从数据中构build一个新的对象,并将其添加到ArrayList。 然后在onPostExecute我想设置listadapter使用ArrayAdapter利用我的ArrayList。

这是我有:

 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<String, Void, ArrayList<Offer>> { private final ProgressDialog dialog = new ProgressDialog(AllOffersListActivity.this); @Override protected void onPreExecute() { this.dialog.setMessage("Fetching offers..."); this.dialog.show(); } @Override protected ArrayList<Offer> doInBackGround(String...urls) { ArrayList<Offer> offerList = new ArrayList<Offer>(); 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<Offer> offerList) { if(this.dialog.isShowing()) this.dialog.dismiss(); setListAdapter(new ArrayAdapter<Offer>(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 ""; } } } 

我看到两个错误。 一个是在我的私人Async类: "The type AllOffersListActivity.CreateArrayListTask must implement the inherited abstract method AsyncTask<String,Void,ArrayList<Offer>>.doInBackground(String...)"

其次,在我的doInBackGround覆盖,我得到: The method doInBackGround(String...) of type AllOffersListActivity.CreateArrayListTask must override or implement a supertype method

我在这里错过了什么?

Solutions Collecting From Web of "填充ListView w / AsyncTask"

这只是一个小小的错字; 应该是doInBackground而不是doInBackGround

@LuxuryMode你doInBackGround做了错误

正确的拼写是doInBackground

asynctask必须执行doInBackground方法,所以它不能识别这个方法,因为错误的方法的名称,所以它给你错误

  The method doInBackGround(String...) of type AllOffersListActivity.CreateArrayListTask must override or implement a supertype method