我掙扎着一個錯誤,告訴我它不能將字符串值轉換爲JSONObject。我試圖將CSRFToken作爲參數傳遞給我的JSONObjectRequest。我不知道這是否是正確的解決方法。類型字符串的值不能轉換爲JSONObject
我收到錯誤消息:org.json.JSONException: Value inz9J9w0N5qASdJO8soWFBZ4UrEpdnjnpmxvFbJ2 of type java.lang.String cannot be converted to JSONObject
相關代碼如下。
HashMap<String, String> params = new HashMap<>();
params.put("_token", ((TestApplication) getApplication()).getCSRFToken());
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this).create();
alertDialog.setTitle("Alert");
Log.i("response", response.toString());
alertDialog.setMessage(response.toString());
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage(error.getMessage());
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
queue.add(stringRequest);
有誰知道我可能做錯了什麼,以及如何糾正這個問題?
不.getCSRFToken()返回正是 「inz9J9w0N5qASdJO8soWFBZ4UrEpdnjnpmxvFbJ2」 還是有額外的字符,如引號或大括號? – phatfingers