I am posting some data into the database using Volley and I get the following jsonarray response.
我使用Volley将一些数据发布到数据库中,我得到以下jsonarray响应。
[
{
"nickname":"panikos",
"username":"panikos@gmail.com",
"user_type":"LEADER",
"latest_steps":"0"
}
]
This is a sample of my code that unfortunately doesn't log out or debug the variable of "nickname" object:(.
这是我的代码示例,遗憾的是它没有注销或调试“nickname”对象的变量:(。
final JsonArrayRequest jsonObjReq1 = new
JsonArrayRequest(AppConfig.URL_GET_TEAM, jsonObject,
new com.android.volley.Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("TAG", response.toString());
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0;i<jsonArray.length();i++){
JSONObject jresponse =
jsonArray.getJSONObject(i);
String nickname =
jresponse.getString("nickname");
Log.d("nickname",nickname);
}
} catch (JSONException e) {
e.printStackTrace();
}
//pDialog.dismiss();
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
//pDialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
Any ideas? Am I missing something?
有任何想法吗?我错过了什么吗?
Thanks.
谢谢。
2 个解决方案
#1
8
I the problem might be - you are already getting response
as a JSONArray
.
我的问题可能是 - 你已经得到了JSONArray的响应。
So, you can Call
所以,你可以打电话
JSONObject jresponse = response.getJSONObject(0);
JSONObject jresponse = response.getJSONObject(0);
and if you have more than 1 object in response, then
如果您有多个对象作为响应,那么
for(int i = 0; i < response.length(); i++){
JSONObject jresponse = response.getJSONObject(i);
String nickname = jresponse.getString("nickname");
Log.d("nickname", nickname);
}
Remove this :
删除这个:
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0;i<jsonArray.length();i++){
JSONObject jresponse =
jsonArray.getJSONObject(i);
String nickname =
jresponse.getString("nickname");
Log.d("nickname",nickname);
}
} catch (JSONException e) {
e.printStackTrace();
}
and add :
并添加:
try {
JSONObject jresponse = response.getJSONObject(0);
String nickname = jresponse.getString("nickname");
Log.d("nickname",nickname);
}catch (JSONException e) {
e.printStackTrace();
}
The Code looks good, however i think you might be missing a call to add jsonObjReq1
in the request queue. I would suggest to use Singleton Pattern.
代码看起来不错,但我认为你可能会错过在请求队列中添加jsonObjReq1的调用。我建议使用Singleton Pattern。
#2
1
Fixed!!!
固定!!!
@Override
public void onResponse(JSONArray response) {
Log.d("TAG", response.toString());
try {
Log.d("JsonArray",response.toString());
for(int i=0;i<response.length();i++){
JSONObject jresponse = response.getJSONObject(i);
String nickname = jresponse.getString("nickname");
Log.d("nickname",nickname);
}
} catch (JSONException e) {
e.printStackTrace();
}
//pDialog.dismiss();
}
There was no need to create a new JSONArray. It was created inside the onResponse() method. The next project I am assigned to do is going to have more complicate webservices.omg!!!
无需创建新的JSONArray。它是在onResponse()方法中创建的。我被指派做的下一个项目将会有更复杂的webservices.omg!
#1
8
I the problem might be - you are already getting response
as a JSONArray
.
我的问题可能是 - 你已经得到了JSONArray的响应。
So, you can Call
所以,你可以打电话
JSONObject jresponse = response.getJSONObject(0);
JSONObject jresponse = response.getJSONObject(0);
and if you have more than 1 object in response, then
如果您有多个对象作为响应,那么
for(int i = 0; i < response.length(); i++){
JSONObject jresponse = response.getJSONObject(i);
String nickname = jresponse.getString("nickname");
Log.d("nickname", nickname);
}
Remove this :
删除这个:
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0;i<jsonArray.length();i++){
JSONObject jresponse =
jsonArray.getJSONObject(i);
String nickname =
jresponse.getString("nickname");
Log.d("nickname",nickname);
}
} catch (JSONException e) {
e.printStackTrace();
}
and add :
并添加:
try {
JSONObject jresponse = response.getJSONObject(0);
String nickname = jresponse.getString("nickname");
Log.d("nickname",nickname);
}catch (JSONException e) {
e.printStackTrace();
}
The Code looks good, however i think you might be missing a call to add jsonObjReq1
in the request queue. I would suggest to use Singleton Pattern.
代码看起来不错,但我认为你可能会错过在请求队列中添加jsonObjReq1的调用。我建议使用Singleton Pattern。
#2
1
Fixed!!!
固定!!!
@Override
public void onResponse(JSONArray response) {
Log.d("TAG", response.toString());
try {
Log.d("JsonArray",response.toString());
for(int i=0;i<response.length();i++){
JSONObject jresponse = response.getJSONObject(i);
String nickname = jresponse.getString("nickname");
Log.d("nickname",nickname);
}
} catch (JSONException e) {
e.printStackTrace();
}
//pDialog.dismiss();
}
There was no need to create a new JSONArray. It was created inside the onResponse() method. The next project I am assigned to do is going to have more complicate webservices.omg!!!
无需创建新的JSONArray。它是在onResponse()方法中创建的。我被指派做的下一个项目将会有更复杂的webservices.omg!