我按照AndroidHive教程实施了Volley。我创建了两个类(AppController,LruBitmapCache)。 我可以使基本查询工作,但当我传递参数时,我收到一个错误:
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("city", "Wien");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, list_all_users_json, new JSONObject(jsonParams), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("JSONJSONJSONJSON", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("JSONJSONJSONJSON", "Error: " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
PHP:
$city = $_POST['city'];
try {
$stmt = $conn->prepare("SELECT USERS.USERNAME
FROM USERS
WHERE USERS.CITY=?");
$stmt -> execute($city);
while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
$output[] = $row;
$response["success"] = 1;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
$response["success"] = 0;
}
$struct = array("Output" => $output);
print(json_encode($struct));
我得到的错误是:
01-16 22:59:59.817: D/Volley(31689): [1] 3.onErrorResponse: JSONJSONJSONJSON
我不是JSON的家伙,所以我的代码显然有问题我只能知道它是什么。