client.post("http://10.0.2.2/project/process/selectalluser.php", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
Integer contacts = controller.getContactsCount();
Log.d("Reading contacts: ", contacts+"");
System.out.println("onSuccess");
JSONArray jsonArray = new JSONArray(response);
Log.d("Reading response: ", response.length()+"");
System.out.println(response);
Toast.makeText(getApplicationContext(), "MySQL DB has been informed about Sync activity", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(int statusCode, Throwable error, String content) {
System.out.println("onFailure");
System.out.println(statusCode);
System.out.println(error);
System.out.println(content);
Toast.makeText(getApplicationContext(), "Error Occured", Toast.LENGTH_LONG).show();
}
});
然後我的PHP:字符串轉換成JSON在Android的
$stmt = $dbh->prepare("SELECT * FROM `admin_tbl`");
if ($stmt->execute()) {
$basicinfo = array();
if ($stmt->rowCount() > 0) {
while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$basicinfo[] = array(
'email' => $selected_row['email'],
'username' => $selected_row['username'],
'password' => $selected_row['password'],
'fname' => $selected_row['fname'],
'mname' => $selected_row['mname'],
'lname' => $selected_row['lname'],
'suffix' => $selected_row['suffix'],
'status' => $selected_row['status']);
}
echo json_encode($basicinfo, JSON_UNESCAPED_UNICODE);
//return $basicinfo;
} else {
echo json_encode(array(
'error' => false,
'message' => "No record found!"
));
exit;
}
}
這是我的代碼,我想這是字符串類型,以JSON的,所以我可以算多少條記錄那裏的響應轉換。但我得到的Unhandled Exception: org.json.JSONException
錯誤行JSONObject json = new JSONObject(response);
線System.out.println(response);
將導致如下:
I /的System.out:[{ 「電子郵件」: 「[email protected]」, 「用戶名」: 「管理員」, 「密碼」: 「管理」,其中 「fname」: 「布拉德利」, 「MNAME」: 「Buenafe」, 「L-NAME」: 「Dalina」, 「後綴」: 「」, 「狀態」: 「1」 }]
如何字符串格式正確轉換成JSON
我需要有一個特定的進口對於我不是能提到這一點,但我想這已經 –
'org.json.JSONArray'? –
我認爲不會有像上面那樣的錯誤 –