我正在開發一個使用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獲取數據,因爲它消耗了我很多時間
是你試圖檢查你的JSON數組長度? –
不,我想從數組中獲取數據並將其顯示在listfragment中,方法是將它傳遞給Model類,通過列表 – Nyerere
您不瞭解我...您是否檢查服務器的答案是否正確?什麼是數組長度? –