我试图用我数据库中的数据填充listView。我有tbl_table只有tableID和tableCapacity。我试着运行我的phpcodes到postman,并在日志中打印,似乎没有问题,所以我认为它必须是因为我转换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);
答案 0 :(得分:0)
response
变量应该是一个合适的json字符串,但它的格式不正确。根据错误消息并且预期它是一个json数组,这个json字符串应该以数组符号开头,即[
,并以]
结束。
我建议你在这一行使用断点并评估response
变量的值。您可以使用online json formatters/validators找出此json字符串中的错误。然后你应该改变创建这个json的方法的逻辑。