Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
319 views
Welcome To Ask or Share your Answers For Others

1 Answer

Volley works in an asynchronous way, it means that you can't know when the response will arrive from your webservice. It will works on separate thread rather than UI thread.

So the outside block of code will get executed irrespective of the Volley response.

If you need the string in your function you should probably create a method and call it when you have the result.

Here is an example:

    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");
                passdata(medicine_description);  //create method to pass data
                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();
            }

        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...