0

I am trying to make a small app that retrieves some JSON from a server that is dependent on user input.

Thus far I have the following to work:

  • User enters search term
  • App finds search results

The problem I have just now is parsing the data in my main class once the results have been retrieved.

My Code: Below is the important code. I have not included the method that parseses or retrieves the data as these do work.

Basically I need a way to send a "done" signal or something to the method that calls my ResultsGetter.getToday() method once the data has finished being retrieved as when I try to access the data as is, it is all null as the data hasn't finished yet.

Main Activity

public class MainActivity extends Activity implements android.view.View.OnClickListener{

    ResultsGetter results;
        HttpClient client;

    EditText location;
    Button search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        client = new DefaultHttpClient();

        location = (EditText) findViewById(R.id.etLocation);
        search = (Button) findViewById(R.id.btSearch);
        search.setOnClickListener(this);

        search= new ResultsGetter(
                this,
                0,
                "MY DATA URL",
                client);

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    results.getToday(location.getText().toString());


    }



}

My ResultsGetter class

public class ResultsGetter {

public ResultsGetter(Context c, int f, String url, HttpClient client) {
        this.context = c;
        this.term = f;
        this.url = url;

        this.client = client;

    }

public void getToday(String location) {

        getData = new Reader();
        builtUrl = url + "?length=today&city=" + location;
        getData.execute();

}

public class Reader extends AsyncTask<String, Integer, JSONObject> {

        @Override
        protected JSONObject doInBackground(String... params) {
            // TODO Auto-generated method stub

            try {

                alldata = getData();

                return alldata;

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                Log.e("Error", "CLIENT Exception " + e.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("Error", "IOException " + e.toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.e("Error", "JSON Exception " + e.toString());
            }

            return null;

        }




        @Override
        protected void onPostExecute(JSONObject result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            Log.e("Success", "JSON retrieved");
            parseData();

        }

    }

}
nmyster
  • 454
  • 1
  • 7
  • 20

1 Answers1

0

Store the AsyncTask instance then call cancel on it when you want it to stop executing, then you can check isCancelled() in the doInBackground to end early. And onPostExecute won't run if the task is cancelled before completion.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • But how do I check for this from my main activity? where i call results.getToday() etc... I want to wait until that has completly finished before moving on as by that point the data will be retrieved and string variables within ResultGetter will have the value of the JSON data. – nmyster Sep 30 '13 at 10:55
  • Well if you want to do something when the the `AsyncTask` is done you would need to run it from `onPostExecute`. You could make a interface for the task that you would pass to it on creation which you would define in the `Activity` and then you would call the particular method in the interface from `onPostExecute`. – Emil Davtyan Sep 30 '13 at 11:02
  • Here is an example of what I was describing above : http://stackoverflow.com/a/9963705/220710 – Emil Davtyan Sep 30 '13 at 11:02