我正在开发一个使用android的应用程序,我试图在我的DataListLoader类中使用loadInBackground()方法获取json数组显示在Listfragment中:public List android中的loadInBackground()方法没有从json中获取数据
class DataListLoader extends AsyncTaskLoader> {
List mModels;
String jsonString;
String name = "Daniel Nyerere", phone = "0652400670";
public DataListLoader(Context context) {
super(context);
loadInBackground();
}
public List 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 entries = new ArrayList(50);
StringRequest stringRequest = new StringRequest(
Request.Method.GET, "http://10.0.2.2/webapp/json_get_data.php", new Response.Listener() {
@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获取数据,因为它消耗了我很多时间
2016-05-29
Nyerere
+0
是你试图检查你的JSON数组长度? –
+0
不,我想从数组中获取数据并将其显示在listfragment中,方法是将它传递给Model类,通过列表 –
+0
您不了解我...您是否检查服务器的答案是否正确?什么是数组长度? –