Wypełnianie widoku listy w / AsyncTask

To prawdopodobnie nie jest zbyt eleganckie, ale próbuję połączyć się z usługą internetową, pobrać plik JSON, przetworzyć go, utworzyć z niego obiekt, dodać ten obiekt do ArrayList, a następnie użyć tej ArrayList, aby wypełnić mój ListView.

Próbuję to wszystko zrobić z AsyncTask.

Podsumowanie: doInBackgroud pobiera ciąg adresu url i używa go do łączenia się z usługą internetową. Dostaję dane JSON jako ciąg znaków, analizuję je, konstruuję nowy obiekt z danych i dodaję go do ArrayList. Następnie w onPostExecute próbuję ustawić listadapter używając ArrayAdapter, który wykorzystuje moją ArrayList.

Oto co mam:

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 "";
        }

    }

}
Widzę dwa błędy. Jeden jest na mojej prywatnej klasie Async: "The type AllOffersListActivity.CreateArrayListTask must implement the inherited abstract method AsyncTask<String,Void,ArrayList<Offer>>.doInBackground(String...)"

Po Drugie, na moim doInBackGround Override, dostaję: The method doInBackGround(String...) of type AllOffersListActivity.CreateArrayListTask must override or implement a supertype method

Co mi umyka?
Author: LuxuryMode, 2011-05-13

2 answers

To tylko mała literówka; powinno być doInBackground zamiast doInBackGround.

 7
Author: Niv Steingarten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-13 03:39:47

@LuxuryMode popełniłeś błąd na doInBackGround

Poprawna pisownia to doInBackground

Asynctask musi zaimplementować metodę doInBackground więc nie rozpoznaje tej metody z powodu złej nazwy metody więc daje błąd

 The method doInBackGround(String...) of type AllOffersListActivity.CreateArrayListTask must 
override or implement a supertype method
 2
Author: Dharmendra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-13 03:53:12