How to access data outside onResponse?

余生颓废 提交于 2019-12-14 04:12:09

问题


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

回答1:


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

        }



回答2:


onResponse is invoked asynchronously when the Request is finished, hence after your logging output. The Request Queue doesn't block your current Thread.



来源:https://stackoverflow.com/questions/44392186/how-to-access-data-outside-onresponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!