error sending data to server using volley library

主宰稳场 提交于 2019-12-24 11:36:09

问题


I have not used volley library much. i have read the tutorials. i want to send data to a url which which will enter that data in database. i have tried the following code but its not working. data is not entered in the database.

  String url = "http://tipseducation.com/system/eadmin/insertschedule/";
        StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //Valid Response
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //error message
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();
                params.put("appt_name", ed_name);
                params.put("appt_email", ed_email);
                params.put("appt_contact", ed_contact);
                params.put("appt_date", ed_date);
                params.put("appt_time", ed_time);
                params.put("appt_service", ed_spinner);
                return params;
            }
        };

can anyone please help me. iam new to this


回答1:


Instead of getHeaders, you should use getBody for your POST request.

You can refer to my following working sample code (replace my JSONObject and Url by yours). Hope this helps!

        ...   
        try {
            RequestQueue queue = Volley.newRequestQueue(this);
            jsonBody = new JSONObject();
            jsonBody.put("Title", "Android Volley POST DATA Demo");
            jsonBody.put("Author", "BNK");
            jsonBody.put("Date", "2015/09/17");
            requestBody = jsonBody.toString();

            StringRequest stringRequest = new StringRequest(1, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // do something...
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // do something...
                }
            }) {
                @Override
                public String getBodyContentType() {
                    return "application/json; charset=utf-8";
                }

                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {
                        return requestBody == null ? null : requestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {
                        e.printStackTrace();
                        return null;
                    }
                }
            };
            queue.addToRequestQueue(stringRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ...


来源:https://stackoverflow.com/questions/32600209/error-sending-data-to-server-using-volley-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!