我是JSON和Android的新手 我做简单的应用程序,我想从mysql中的数据库请求数据,我得到这个例外
这是我的代码
final GetDataFromDB getdb = new GetDataFromDB();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.maintable);
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<Users> users = parseJSON(data);
addData(users);
}
});
}
}).start();
}
public ArrayList<Users> parseJSON(String result) {
ArrayList<Users> users = new ArrayList<Users>();
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray jArray = jsonObj.getJSONArray("users");
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setJenis_kendaraan(json_data.getString("jenis_kendaraan"));
user.setBarang_sitaan(json_data.getString("barang_sitaan"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
这里是Users.java类
public class Users {
int id;
String jenis_kendaraan;
String barang_sitaan;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getJenis_kendaraan() {
return jenis_kendaraan;
}
public void setJenis_kendaraan(String jenis_kendaraan) {
this.jenis_kendaraan = jenis_kendaraan;
}
public String getBarang_sitaan() {
return barang_sitaan;
}
public void setBarang_sitaan(String barang_sitaan) {
this.barang_sitaan = barang_sitaan;
}
}
这是GetDataFromDB.java
public class GetDataFromDB {
public String getDataFromDB() {
try {
HttpPost httppost;
HttpClient httpclient;
httpclient = new DefaultHttpClient();
httppost = new HttpPost(
"http://192.168.0.102/koneksi/getdata.php"); // change this to your URL.....
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost,
responseHandler);
return response.trim();
} catch (Exception e) {
System.out.println("ERROR : " + e.getMessage());
return "error";
}
}
}
这是我的PHP代码,(我正在使用AMPPS)
<?php
mysql_connect("localhost","root","mysql"); // host, username, password...
mysql_select_db("tilang"); // db name...
$q=mysql_query("SELECT id, jenis_kendaraan, barang_sitaan FROM pelanggaran");
while($row=mysql_fetch_assoc($q))
$json_output[]=$row;
print(json_encode($json_output));
mysql_close();
?>
logcat的
06-03 10:01:22.795 30366-30366/com.example.indraaaeff.historyapp E/log_tag﹕ Error parsing data org.json.JSONException: Value error of type java.lang.String cannot be converted to JSONArray
这是我从php文件中获取的字符串。
[{"pid":"1","jenis_kendaraan":"Motor","barang_sitaan":"SIM"},
{"pid":"2","jenis_kendaraan":"Mobil","barang_sitaan":"Lain-lain"},
{"pid":"3","jenis_kendaraan":"Motor","barang_sitaan":"SIM"}]
请帮我删除
答案 0 :(得分:0)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.maintable);
new loadUserTask().execute();
}
public class loadUserTask extends AsyncTask<Void,Void,ArrayList<User>> {
@Override
protected Void doInBackground(Void... params) {
data = getdb.getDataFromDB();
System.out.println(data);
ArrayList<Users> users = parseJSON(data);
return users;
}
@Override
protected void onPostExecute(ArrayList<User> users) {
addData(users);
}
}
public ArrayList<Users> parseJSON(String result) {
ArrayList<Users> users = new ArrayList<Users>();
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray jArray = jsonObj.getJSONArray("users");
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setJenis_kendaraan(json_data.getString("jenis_kendaraan"));
user.setBarang_sitaan(json_data.getString("barang_sitaan"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}