So, I'm writing an android app. During OnCreate() of an activity, I request some JSONObjects via HTTP GET. That method has a response listener that, well, does something when it gets a JSONObject. In the OnCreate-method, I need to use the response, so can I somehow set it up so that the OnCreate method waits until the response listener does it's job? Do I have to use multithreading? Here's the code I'm talking about:
Here's the call in OnCreate():
...
// Instantiate the RequestQueue.
final RequestQueue queue = Volley.newRequestQueue(this);
executeJson(QUERY_URL);
System.out.println(jsonresponse);
...
Here's the called method:
private void executeJson(String url) {
SharedPreferences prefs = this.getSharedPreferences("AppPref", MODE_PRIVATE);
final String token = prefs.getString("token", null);
RequestQueue queue = Volley.newRequestQueue(this);
Map<String, String> params = new HashMap<String, String>();
System.out.println(token);
params.put("access_token", token);
CustomRequest jsonRequest = new CustomRequest(url, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
jsonresponse = response;
}
}, this.createRequestErrorListener());
System.out.println(jsonRequest);
queue.add(jsonRequest);
}
I realize that a simple Thread.Sleep() would, well, make my app go to sleep entirely, so that can't be the solution.
The intention of this activity is to be a "Loading Activity" - It pops up once the user wants to access his data and creates an intent to the data activity, destroying itself after the data is loaded.