简体   繁体   English

字符串类型的值不能转换为JSONObject

[英]Value of type String cannot be converted to JSONObject

I'm struggling with an error that tells me that it cannot convert a String value to JSONObject. 我正在努力解决一个错误,该错误告诉我它无法将String值转换为JSONObject。 I'm trying to pass a CSRFToken as a parameter to my JSONObjectRequest. 我试图将CSRFToken作为参数传递给我的JSONObjectRequest。 I have no clue if this is the right way to even go about it. 我不知道这是否是解决问题的正确方法。

I'm getting the error message: org.json.JSONException: Value inz9J9w0N5qASdJO8soWFBZ4UrEpdnjnpmxvFbJ2 of type java.lang.String cannot be converted to JSONObject 我收到错误消息: org.json.JSONException: Value inz9J9w0N5qASdJO8soWFBZ4UrEpdnjnpmxvFbJ2 of type java.lang.String cannot be converted to JSONObject

Relevant code is below. 相关代码如下。

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);

Does anyone know what I may have done wrong and how to rectify the issue? 有谁知道我可能做错了什么以及如何纠正该问题?

The following should be valid. 以下内容应有效。

{
    "_token": "inz9J9w0N5qASdJO8soWFBZ4UrEpdnjnpmxvFbJ2"
}

Can you try 你能试一下吗

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>() {
// ...
}

I've figured it out. 我知道了。 JsonObjectRequest doesn't send any accept headers. JsonObjectRequest不发送任何接受标头。 After adding an accept header I was able to get the response I was after. 添加accept标头后,我可以得到我想要的响应。

Final code: 最终代码:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM