【发布时间】: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