php - 使用 mysql 数据库中的数据过滤 ListView

标签 php android mysql json listview

我正在尝试更改本教程的 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/

相关文章:

php - 使用 PHP 作为后端的 Flex Remoting 问题

php - 在 OpenBSD 的 Apache 服务器上运行 xinc

PHP/MySQL 多维数组

php - 如何使用php数组制作html合并行表

java - 删除 TextView 内容时禁用按钮

Mysql:添加外键不会在 MyISAM 表上给出警告/错误

php - 在谷歌图表上放置值

android 面临在 android 2.2 中使用 fragment 的问题

java - 解码 : com. google.android.play.core.review.b 时找不到类

mysql - 通过乘以两列来在插入之前触发更新值