Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

android - How to access data outside onResponse?

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

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

1 Reply

0 votes
by (71.8m points)

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

        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...