【问题标题】:How to access data outside onResponse?如何访问 onResponse 之外的数据?
【发布时间】:2017-06-06 14:03:34
【问题描述】:

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

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

【问题讨论】:

    标签: android json android-volley


    【解决方案1】:

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

    因此,无论Volley 响应如何,都会执行外部代码块。

    如果您需要在函数中使用字符串,您可能应该创建一个方法并在获得结果时调用它。

    这是一个例子:

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

    【讨论】:

    • 这称为callback,是处理这种情况的适当方法。
    【解决方案2】:

    onResponse 在请求完成时异步调用,因此 您的日志输出之后。请求队列不会阻塞您当前的线程。

    【讨论】:

      猜你喜欢
      • 2017-06-15
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多