2016-05-29 65 views
0

我正在開發一個使用android的應用程序,我試圖在我的DataListLoader類中使用loadInBackground()方法獲取json數組顯示在Listfragment中:public List <Model> android中的loadInBackground()方法沒有從json中獲取數據

class DataListLoader extends AsyncTaskLoader<List<Model>> { 
List<Model> mModels; 
String jsonString; 
String name = "Daniel Nyerere", phone = "0652400670"; 

public DataListLoader(Context context) { 
    super(context); 
    loadInBackground(); 
} 



public List<Model> loadInBackground() { 
    // You should perform the heavy task of getting data from 
    // Internet or database or other source 
    // Here, we are generating some Sample data 
    // Create corresponding array of entries and load with data. 
    final List<Model> entries = new ArrayList<Model>(50); 
    StringRequest stringRequest = new StringRequest(
      Request.Method.GET, "http://10.0.2.2/webapp/json_get_data.php", new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      if (response.trim().contains("server_response")) { 
       jsonString = response.toString(); 
       try { 
        JSONObject jsonObject = new JSONObject(jsonString); 
        JSONArray jsonArray = jsonObject.getJSONArray("server_response"); 
        int count = 0; 

        while (count < jsonArray.length()) { 
         JSONObject jo = jsonArray.getJSONObject(count); 
         name = jo.getString("name"); 
         phone = jo.getString("user_phone"); 
         entries.add(new Model(name, phone)); 
         System.out.println("DATA FROM JSON ARRAY: " + name + " " + phone); 
         count++; 
         return; 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
    RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue(); 
    requestQueue.add(stringRequest); 
    entries.add(new Model(name, phone)); 
    return entries; 
} 

但是,當我嘗試的System.out.println運行數據( 「數據條目:」 +項);來作爲空列表,所以沒有數據顯示在片段活動中。任何人誰可以幫我修復它,以便我從JSON獲取數據,因爲它消耗了我很多時間

+0

是你試圖檢查你的JSON數組長度? –

+0

不,我想從數組中獲取數據並將其顯示在listfragment中,方法是將它傳遞給Model類,通過列表 – Nyerere

+0

您不瞭解我...您是否檢查服務器的答案是否正確?什麼是數組長度? –

回答

0
//You can call it in UI thread 
final List<Model> entries = new ArrayList<Model>(5); 
    StringRequest stringRequest = new StringRequest(
      Request.Method.GET, "http://10.0.2.2/webapp/json_get_data.php", new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      //you need to update the `ListView` content here 
      //If you need you can create new thread here too e.g. using AsyncTask 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue(); 
requestQueue.add(stringRequest); 
+0

我已經嘗試過,但問題出現在返回的條目變爲null – Nyerere

+0

發佈您的固定代碼。只是我不能從手機上寫代碼 –

相關問題