String类型的值无法转换为JSONObject

时间:2017-07-09 04:15:55

标签: java android

我正在努力解决一个错误,告诉我它无法将String值转换为JSONObject。我正在尝试将CS​​RFToken作为参数传递给我的JSONObjectRequest。我不知道这是否是正确的方法。

我收到错误消息: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);

有谁知道我可能做错了什么以及如何纠正这个问题?

2 个答案:

答案 0 :(得分:0)

以下内容应该有效。

{
    "_token": "inz9J9w0N5qASdJO8soWFBZ4UrEpdnjnpmxvFbJ2"
}

你能试试吗

String tokenString = (String) ((TestApplication) getApplication()).getCSRFToken();
JSONObject params = new JSONObject();
params.put("_token", tokenString);

JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
// ...
}

答案 1 :(得分:0)

我已经弄明白了。 JsonObjectRequest不会发送任何接受标头。添加一个接受标题后,我得到了我所追求的响应。

最终代码:

HashMap<String, String> params = new HashMap<>();

JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        // ...
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this).create();
        alertDialog.setTitle("Alert");
        //alertDialog.setMessage(error.toString());
        alertDialog.setMessage(new String(error.networkResponse.data));
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }
}) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("X-CSRF-TOKEN", ((TestApplication) getApplication()).getCSRFToken());
        params.put("Accept", "application/json");
        return params;
    }
};

queue.add(stringRequest);