【问题标题】:Populating ListView with data from mySQL database使用 mySQL 数据库中的数据填充 ListView
【发布时间】:2016-08-13 11:54:36
【问题描述】:

我试图用我的数据库中的数据填充我的 listView。我有只有 tableID 和 tableCapacity 的 tbl_table。我尝试将我的phpcodes运行到邮递员,并在日志中打印,似乎没有问题,所以我认为这一定是因为我转换Json编码的方式。我不断收到此错误。在此先感谢.. 我在 android 4.1.2 API 16 中运行我的代码。

    FATAL EXCEPTION: main

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1
    at com.google.gson.Gson.fromJson(Gson.java:815)
    at com.google.gson.Gson.fromJson(Gson.java:768)
    at com.google.gson.Gson.fromJson(Gson.java:717)
    at com.kosalgeek.android.json.JsonConverter.toArrayList(JsonConverter.java:42)
    at markjon.nobleza.qaldi.Table.onResponse(Table.java:51)                                                               
    at markjon.nobleza.qaldi.Table.onResponse(Table.java:20)

这是我的代码:

Table.class

public class Table extends AppCompatActivity implements Response.Listener<String> {

    final String TAG = this.getClass().getSimpleName();

    ListView tableLV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table);

        String URL = "http://192.168.1.10/myDB/table.php";

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, this, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Error retrieving data", Toast.LENGTH_SHORT).show();
            }
        });

        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

        tableLV = (ListView)findViewById(R.id.tableList);

    }

    @Override
    public void onResponse(String response) {
        Log.d(TAG, response);


        ArrayList<myTable> tableList = new JsonConverter<myTable>().toArrayList(response, myTable.class);

        BindDictionary<myTable> dict = new BindDictionary<>();
        dict.addStringField(R.id.tableNum, new StringExtractor<myTable>() {
            @Override
            public String getStringValue(myTable item, int position) {
                return "" + item.tableID;
            }
        });

        dict.addStringField(R.id.tableCap, new StringExtractor<myTable>() {
            @Override
            public String getStringValue(myTable item, int position) {
                return "" + item.tableCapacity;
            }
        });

        FunDapter<myTable> adapter = new FunDapter<>(getApplicationContext(), tableList, R.layout.table_layout, dict);

        tableLV.setAdapter(adapter);

    }
}

myTable.class

public class myTable {

    @SerializedName("tableID")
    public int tableID;
    @SerializedName("tableCapacity")
    public int tableCapacity;

}

这两行的错误点:

public class Table extends AppCompatActivity implements Response.Listener<String>

ArrayList<myTable> tableList = new JsonConverter<myTable>().toArrayList(response, myTable.class);

【问题讨论】:

    标签: android mysql json listview gson


    【解决方案1】:

    response 变量应该是正确的 json 字符串,但格式不正确。根据错误消息,由于它应该是一个 json 数组,所以这个 json 字符串应该以数组符号开头,即[,并以] 结尾。 我建议您在这一行上使用一个断点并评估response 变量的值。您可以使用online json formatters/validators 找出这个 json 字符串中有什么问题。然后你应该改变创建这个json的方法的逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 2011-05-27
      相关资源
      最近更新 更多