[英]Android-volley: how do I take the String response value and assign it outside of a volley StringRequest method?
The data appears as expected when I run the method in the debugger, however whenever I try to do anything with it the value of parentObject is returned as null. 当我在调试器中运行方法时,数据按预期方式显示,但是,每当我尝试对其执行任何操作时,parentObject的值都将返回为null。
I simply want to take the response from the server and store it as a JSONObject to be reused elsewhere. 我只是想从服务器获取响应并将其存储为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);
}
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();
}
}
First of all you need to check what type response you getting from server ie (JsonObject or JsonArray). 首先,您需要检查从服务器获得的类型响应,即(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);
}
as per your question ... you want to store response in jsonObject but in code you are store response in jsonArray 根据您的问题...您想将响应存储在jsonObject中,但是在代码中您将响应存储在jsonArray中
parentObject = new JSONArray(response); parentObject =新的JSONArray(response);
so you must have to confirm response is in object or array. 因此,您必须确认响应位于对象或数组中。 if response is in object (like {}) change to parentObject = new JSONObject(response);
如果响应在对象中(例如{}), parentObject = new JSONObject(response);
改为parentObject = new JSONObject(response);
else change type of parentObject to JSONArray
否则将parentObject的类型更改为JSONArray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.