I have no idea why medicine_description
is returning null outside onResponse.
StringRequest stringRequest = new StringRequest(Request.Method.GET, fda_api + "acetaminophen", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject json = new JSONObject(response);
JSONArray jArray = json.getJSONArray("results");
Log.d(TAG, "readJSON: " + jArray.length());
JSONObject json_data = jArray.getJSONObject(0);
medicine_description = json_data.getString("description");
Log.i("THIS ONE IS FINE",medicine_description);
/*for(int i=0; i<jArray.length(); i++){
JSONObject json_data = jArray.getJSONObject(i);
medicine_description = json_data.getString("description");
Log.i("log_tag",medicine_description);
}*/
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
Log.i(TAG, "THIS RETURNS NULL: " + medicine_description);
See Question&Answers more detail:os