如何使用android中的volley将json对象发送到服务器

时间:2022-10-22 20:42:08

I want to send the JSONObject to server using POST method . I have used volley library to pass the string params its working fine, but if i try to use json object its showing an error for calling the json object here is my code

我想使用POST方法将JSONObject发送到服务器。我已经使用volley库传递字符串params它的工作正常,但如果我尝试使用json对象,它显示错误调用json对象这里是我的代码

    private void makeJsonObjReq() {
    showProgressDialog();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Const.URL_LOGIN, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    msgResponse.setText(response.toString());
                    hideProgressDialog();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hideProgressDialog();
                }
            }) {

        /**
         * Passing some request headers
         * */
        @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;
        }

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("un", "xyz@gmail.com");
            params.put("p", "somepasswordhere");
            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);

    // Cancelling request
    // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);       
}

And my Error form server is : [10031] BasicNetwork.performRequest: Unexpected response code 401

我的错误表单服务器是:[10031] BasicNetwork.performRequest:意外的响应代码401

How to solve this problem. I want to add application/json;charset=utf-8 in header please check my code whether its correct or not . Please give me a suggestion to over come this problem

如何解决这个问题呢。我想在标题中添加application / json; charset = utf-8请检查我的代码是否正确。请给我一个建议来解决这个问题

1 个解决方案

#1


24  

Third parameter in JsonObjectRequest is for passing post parameters in jsonobject form. And for header you need to send two separate values one for content-type one for charset.

JsonObjectRequest中的第三个参数用于以jsonobject形式传递post参数。对于标题,您需要发送两个单独的值,一个用于内容类型,一个用于charset。

  RequestQueue queue = Volley.newRequestQueue(this);

  private void makeJsonObjReq() {
    showProgressDialog();


            Map<String, String> postParam= new HashMap<String, String>();
            postParam.put("un", "xyz@gmail.com");
            postParam.put("p", "somepasswordhere");


    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Const.URL_LOGIN, new JSONObject(postParam),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    msgResponse.setText(response.toString());
                    hideProgressDialog();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hideProgressDialog();
                }
            }) {

        /**
         * Passing some request headers
         * */
        @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;
        }



    };

    jsonObjReq.setTag(TAG);
    // Adding request to request queue
    queue.add(jsonObjReq);

    // Cancelling request
    /* if (queue!= null) {
    queue.cancelAll(TAG);
    } */

}

#1


24  

Third parameter in JsonObjectRequest is for passing post parameters in jsonobject form. And for header you need to send two separate values one for content-type one for charset.

JsonObjectRequest中的第三个参数用于以jsonobject形式传递post参数。对于标题,您需要发送两个单独的值,一个用于内容类型,一个用于charset。

  RequestQueue queue = Volley.newRequestQueue(this);

  private void makeJsonObjReq() {
    showProgressDialog();


            Map<String, String> postParam= new HashMap<String, String>();
            postParam.put("un", "xyz@gmail.com");
            postParam.put("p", "somepasswordhere");


    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Const.URL_LOGIN, new JSONObject(postParam),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    msgResponse.setText(response.toString());
                    hideProgressDialog();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hideProgressDialog();
                }
            }) {

        /**
         * Passing some request headers
         * */
        @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;
        }



    };

    jsonObjReq.setTag(TAG);
    // Adding request to request queue
    queue.add(jsonObjReq);

    // Cancelling request
    /* if (queue!= null) {
    queue.cancelAll(TAG);
    } */

}