2017-03-08 102 views
1

我需要从mysql数据库中检索json对象列表,使用volley解析它并使用它填充listview。我是新来的PHP和凌空。以下是我的尝试。使用volley检索json对象列表

JSON:

{"result":[{"id":"3133482000","name":"John Doe","class":"3A","parent":"[email protected]","status":"0","stopid":"6","busno":"0"},{"id":"0","name":"","class":"","parent":"","status":"0","stopid":"0","busno":"0"},{"id":"334","name":"sam","class":"3a","parent":"","status":"0","stopid":"2","busno":"0"}]} 

安卓

JSONArray sts; 
public static final String STUDENT_URL = "http://www.smartlbus.esy.es/getAllStudents.php"; 
     students=new ArrayList<Student>(); 
       StringRequest stringRequest = new StringRequest(STUDENT_URL, new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         try { 
           JSONObject jsonObject=null; 
           jsonObject = new JSONObject(response); 
           sts = jsonObject.getJSONArray(JSON_ARRAY); 

           for (int i = 0; i < sts.length(); i++) { 
            JSONObject jo = sts.getJSONObject(i); 
            Student s=new Student(); 
            Toast.makeText(StudentDetails.this,jo.getString(STUDENT_NAME),Toast.LENGTH_SHORT).show(); 
            s.setId(jo.getString(STUDENT_ID)); 
            s.setName(jo.getString(STUDENT_NAME)); 
            s.setClassNo(jo.getString(STUDENT_CLASS)); 
            s.setBusno(jo.getString(STUDENT_BUS)); 
            s.setStatus(Integer.parseInt(jo.getString(STUDENT_STATUS))); 
            s.setStopid(jo.getString(STUDENT_STOP)); 
            students.add(s); 
           } 
         } 
         catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         error.printStackTrace(); 
        } 
       }); 


       RequestQueue requestQueue = Volley.newRequestQueue(this); 
       requestQueue.add(stringRequest); 
       listView.setAdapter(new CustomListAdapter(StudentDetails.this,students)); 

请帮助我。如果这里不符合标准,请发表评论并告诉我。我会把它记下来。

+0

尝试使用GSON库用于解析JSON –

+0

您的网址不返回任何JSON响应 –

+0

我固定的URL。请再检查一次。 –

回答

0

尝试使用Gson来简化您的开发。

创建您的Java POJO类:

public class StudentList { 
    List<Student> results; 
    // setter 

    public static class Student { 
      String name; 
      String id; 
      String status; 
      String class; 
      String stopid; 
      String busno; 
     // getter 
    } 
} 

然后在onResponse()方法使用GSON解析。 我建议使用Retrofit进行网络通话。

Gson gson = new Gson(); 
StudentList list = gson.fromJson(response); 
// populate to ListView using list.getResults() 
0

试试这个&检查您的网址。你的网址不会返回任何json响应。

 StringRequest stringRequest = new StringRequest(Request.Method.GET,STUDENT_URL , 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         try { 
           Log.d("TAG","response :"+response); 
           JSONObject jsonObject = new JSONObject(response); 
           sts = jsonObject.getJSONArray(JSON_ARRAY); 

           for (int i = 0; i < sts.length(); i++) { 
            JSONObject jo = sts.getJSONObject(i); 
            Student s=new Student(); 
            Log.d("TAG","Name :"+jo.getString(STUDENT_NAME)); 
            // Toast.makeText(StudentDetails.this,jo.getString(STUDENT_NAME),Toast.LENGTH_SHORT).show(); 
            s.setId(jo.getString(STUDENT_ID)); 
            s.setName(jo.getString(STUDENT_NAME)); 
            s.setClassNo(jo.getString(STUDENT_CLASS)); 
            s.setBusno(jo.getString(STUDENT_BUS)); 
            s.setStatus(Integer.parseInt(jo.getString(STUDENT_STATUS))); 
            s.setStopid(jo.getString(STUDENT_STOP)); 
            students.add(s); 
           } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         e.printStackTrace(); 
        } 
       }); 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 
+0

我已经解决了URL的问题。代码仍然不起作用。 –