2016-08-13 107 views
0

我试图用我的数据库中的数据填充我的listView。我只有tableID和tableCapacity的tbl_table。我试图运行我的phpcodes邮递员,并在日志中打印,似乎没有问题,所以我认为它必须是因为我转换Json编码的方式。我不断收到这个错误。在此先感谢..即时通讯运行我的代码中的Android 4.1.2 API 16从mySQL数据库填充ListView与数据

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

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