如何在onResponse之外访问数据? [英] How to access data outside onResponse?

查看:116
本文介绍了如何在onResponse之外访问数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么medicine_description在onResponse之外返回null.

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

推荐答案

Volley以异步方式工作,这意味着您不知道响应何时从Web服务到达.它将在单独的线程而不是UI线程上工作.

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.

因此,不管Volley响应如何,都将执行外部代码块.

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.

这里是一个例子:

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

        }

这篇关于如何在onResponse之外访问数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆