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();
}
}
}