2017-03-08 69 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的問題。代碼仍然不起作用。 –