我正在尝试更改本教程的 private void displayListView()
( http://www.mysamplecode.com/2012/07/android-listview-custom-layout-filter.html ),以便我可以检查如果我从 mySql 数据库获取数据,它是否仍然有效。
这是我的 PHP 文件中的 Json 数组:
[{"code":"AFG","name":"Afghanistan","continent":"Asia","region":"Southern and Central Asia"}, {"code":"ALB","name":"Albania","continent":"Europe","region":"Southern Europe"},{"code":"DZA","name":"Algeria","continent":"Africa","region":"Northern Africa"},{"code":"ASM","name":"American Samoa","continent":"Oceania","region":"Polynesia"},{"code":"AND","name":"Andorra","continent":"Europe","region":"Southern Europe"},{"code":"AGO","name":"Angola","continent":"Africa","region":"Central Africa"},{"code":"AIA","name":"Anguilla","continent":"North America","region":"Caribbean"}]
这就是我所做的:
private void displayListView() {
// Creating volley request obj
JsonArrayRequest stringRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Country country = new Country(code, name, continent, region);
country.setCode(obj.getString("code"));
country.setName(obj.getString("name"));
country.setContinent(obj.getString("continent"));
country.setRegion(obj.getString("region"));
// adding country to country array
countryList.add(country);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(stringRequest);
但是我收到错误:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
我不确定我是否以正确的方式解析 JSON,并且无法找出错误。任何帮助将不胜感激。
最佳答案
这是因为您没有从 php.ini 获取应用程序中的任何响应数据。尝试记录从服务调用收到的结果,然后进行解析。
关于php - 使用 mysql 数据库中的数据过滤 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37922865/