因此,对于我的最后一个问题:Creating Json file with C# of mysql data,我尝试从创建的JSON /file中获取数据,但是它只是没有加载请求的页面,并且在Android仿真器上没有出现任何错误。这是我的截击码:
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,因为,我可以得到数据,但不能显示它!
发布于 2019-07-31 11:32:37
在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 (ArrayList,android.app.Activity)不能应用于(ArrayList,匿名com.android.volley.Response.Listener)“
postClass.notifyDataSetChanged();//THIS LINE IS ENOUGH
} catch (JSONException e) {
e.printStackTrace();
}发布于 2019-07-31 11:02:56
这里有几点你必须检查: 1-‘学生’是正确的名字在你的Json,而不是‘学生’。JSON结果对象的结构->Array->Object。
我个人更喜欢使用GSON,因为它不需要手动循环就可以自动转换。
发布于 2019-07-31 11:33:04
我解决了这个问题,我补充道:
allstudents.add(stuName);
postClass.notifyDataSetChanged(); //THİS LİNE SOLVED İThttps://stackoverflow.com/questions/57289138
复制相似问题