【问题标题】:Android-volley: how do I take the String response value and assign it outside of a volley StringRequest method?Android-volley:如何获取 String 响应值并将其分配到 volley StringRequest 方法之外?
【发布时间】:2019-01-10 19:52:06
【问题描述】:

当我在调试器中运行该方法时,数据按预期显示,但是每当我尝试对其执行任何操作时,parentObject 的值都会返回为 null。

我只是想从服务器获取响应并将其存储为 JSONObject 以便在其他地方重用。

public void jsonParse() {
    String url = "http://178.128.166.68/getUserInfo.php";

    StringRequest request = new StringRequest(Request.Method.POST, url,  new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                /*
                the data appears as expected within the debugger however
                 returns null when i attempt to use it elsewhere
                 */
                parentObject = new JSONArray(response);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("phone", phone);
            params.put("password", password);

            return params;
        }
    };
    mQueue.add(request);


}

【问题讨论】:

  • 首先检查你的 JSON 类型是 JSONArray 还是 JSONObject
  • JSONObject json = new JSONObject(response);

标签: java android json android-volley


【解决方案1】:
StringRequest request = new StringRequest(Request.Method.POST, url,  new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try {
            JSONObject obj = new JSONObject(response);
            parentObject = new JSONArray(obj);
            for (int i = 0; i < parentObject.length(); i++) {
                JSONObject parObj = (JSONObject) parArray.get(i);
                ModelClass modelClass = new ModelClass()// you have to create a model 
                modelClass.set1Parameter(parObj.getString("1Parameter"));
                modelClass.set2Parameter(parObj.getString("2Parameter"));
                arrayList.add(modelClass)//In your arraylist save the data


            }



        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

【讨论】:

    【解决方案2】:

    首先,您需要检查从服务器获得的响应类型,即(JsonObject 或 JsonArray)。

    public void jsonParse() {
        String url = "http://178.128.166.68/getUserInfo.php";
    
        StringRequest request = new StringRequest(Request.Method.POST, url,  new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    /*
                 if its JSONObject
                     */
                      JSONObject jsonobject = new JSONObject(response);
                    String  value= jsonobject.getString("value");
    
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
    
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("phone", phone);
                params.put("password", password);
    
                return params;
            }
        };
        mQueue.add(request);
    
    
    }
    

    【讨论】:

      【解决方案3】:

      根据您的问题...您想将响应存储在 jsonObject 中,但在代码中您将响应存储在 jsonArray 中

      parentObject = new JSONArray(response);

      所以你必须确认响应是在对象或数组中。 如果响应在对象(如 {})中,则更改为 parentObject = new JSONObject(response); 否则将 parentObject 的类型更改为 JSONArray

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-22
        • 2017-09-12
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多