org.json.JSONException:java.lang.String类型的值failure_post无法转换为JSONObject

时间:2015-12-07 06:36:36

标签: android json android-volley jsonobject

我希望使用volly library将数据发送到服务器当我使用volley library向服务器发送数据时出现以下错误:

org.json.JSONException: Value failure_post of type 
                     java.lang.String cannot be converted to JSONObject

这是我的java代码:

public void insertClick(View view)
{
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    HashMap<String, JSONObject> jsonParams = new HashMap<String, JSONObject>();

    JSONObject params = new JSONObject();
    try
    {
        params.put("title","BOOK");
        params.put("intro","hellothere");
    }
    catch (JSONException e)
    {

        Log.i(LABLE, "erorr is : "+ e.getMessage().toString());
    }

    try {

        JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,

        new JSONObject(jsonParams),
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Toast.makeText(getApplicationContext() , " insert ok ",Toast.LENGTH_LONG).show();

            }
        },
        new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error) {
                //   Handle Error

                Log.i(LABLE, "erorr is : "+ error.getMessage().toString());

                Toast.makeText(getApplicationContext() , " insert failed ",Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json");

                return headers;
            }
        };
        queue.add(postRequest);
    }
    catch (Exception e)
    {
        Toast.makeText(this , "erorr in Json Object Request ",Toast.LENGTH_LONG).show();
    }

2 个答案:

答案 0 :(得分:0)

创建JsonArray

将jsonObjects添加到该数组

然后从JsonObjectJsonArray发送完整的JsonObjectRequest广告

答案 1 :(得分:0)

尝试将params代替jsonParams放入JsonObjectRequest,即

替换

JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(jsonParams),...

。通过

JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,params,...

或按照以下方式从地图中检索

jsonParams.get("key")