无法使用 android-volley 从 JSON 数组获取数据到自定义列表视图
Cant get data from JSON array with android-volley into custom listview
根据我的最后一个问题:
我试图从创建的 JSON url/file 中获取数据,但它只是不加载请求的页面并且在 android 模拟器上没有给出任何错误。
这是我的 Volley 代码:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.studentss);
allstudents=new ArrayList<>();
student_lists=findViewById(R.id.students_listview);
postClass=new PostClass(allstudents,this);
student_lists.setAdapter(postClass);
mQueue=Volley.newRequestQueue(this);
jsonParse()
private void jsonParse(){
String url=".......WebForm1.aspx";
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray=response.getJSONArray("Students");
for(int i=0;i<jsonArray.length();i++){
JSONObject stu =jsonArray.getJSONObject(i);
String stuName=stu.getString("firstname");
allstudents.add(stuName);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
如果你能指出我的错误,我将不胜感激。我认为问题必须与 android 而不是 json 相关,因为 我可以获取数据但无法显示它!
以下是您必须检查的一些要点:
1- 'Student' 是您 Json 中的正确名称,而不是 'student'。
2-JSON 结果的结构 Object->Array->Object.
我个人更喜欢使用 GSON,因为它可以自动转换而无需手动循环。
在收到响应后 onResponse()
内,您需要使用更新的数据设置 listView。
在onResponse()
中添加此代码
postClass=new PostClass(allstudents,this);
student_lists.setAdapter(postClass); or postClass.notifyDataSetChanged();
喜欢这个 -
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response . getJSONArray ("Students");
for (int i = 0;i < jsonArray.length();i++){
JSONObject stu = jsonArray . getJSONObject (i);
String stuName = stu . getString ("firstname");
allstudents.add(stuName);
}
postClass=new PostClass(allstudents,this);//THİS LİNE GIVES AN ERROR LIKE THIS BUT ONLY USING THE NEXT LINE WOULD BE EFFICIENT AND ALSO SOLVE THE PROBLEM JUST WANTED TO EDIT
:postClass
(数组列表,
android.app.Activity)
在 PostClass 中无法应用
到
(数组列表,
匿名 com.android.volley.Response.Listener)"
postClass.notifyDataSetChanged();//THIS LINE IS ENOUGH
} catch (JSONException e) {
e.printStackTrace();
}
我通过添加解决了这个问题:
allstudents.add(stuName);
postClass.notifyDataSetChanged(); //THİS LİNE SOLVED İT
根据我的最后一个问题:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.studentss);
allstudents=new ArrayList<>();
student_lists=findViewById(R.id.students_listview);
postClass=new PostClass(allstudents,this);
student_lists.setAdapter(postClass);
mQueue=Volley.newRequestQueue(this);
jsonParse()
private void jsonParse(){
String url=".......WebForm1.aspx";
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray=response.getJSONArray("Students");
for(int i=0;i<jsonArray.length();i++){
JSONObject stu =jsonArray.getJSONObject(i);
String stuName=stu.getString("firstname");
allstudents.add(stuName);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
如果你能指出我的错误,我将不胜感激。我认为问题必须与 android 而不是 json 相关,因为 我可以获取数据但无法显示它!
以下是您必须检查的一些要点: 1- 'Student' 是您 Json 中的正确名称,而不是 'student'。 2-JSON 结果的结构 Object->Array->Object.
我个人更喜欢使用 GSON,因为它可以自动转换而无需手动循环。
在收到响应后 onResponse()
内,您需要使用更新的数据设置 listView。
在onResponse()
postClass=new PostClass(allstudents,this);
student_lists.setAdapter(postClass); or postClass.notifyDataSetChanged();
喜欢这个 -
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response . getJSONArray ("Students");
for (int i = 0;i < jsonArray.length();i++){
JSONObject stu = jsonArray . getJSONObject (i);
String stuName = stu . getString ("firstname");
allstudents.add(stuName);
}
postClass=new PostClass(allstudents,this);//THİS LİNE GIVES AN ERROR LIKE THIS BUT ONLY USING THE NEXT LINE WOULD BE EFFICIENT AND ALSO SOLVE THE PROBLEM JUST WANTED TO EDIT
:postClass (数组列表, android.app.Activity) 在 PostClass 中无法应用 到 (数组列表, 匿名 com.android.volley.Response.Listener)"
postClass.notifyDataSetChanged();//THIS LINE IS ENOUGH
} catch (JSONException e) {
e.printStackTrace();
}
我通过添加解决了这个问题:
allstudents.add(stuName);
postClass.notifyDataSetChanged(); //THİS LİNE SOLVED İT