错误使用凌空库将数据发送到服务器 [英] error sending data to server using volley library

查看:189
本文介绍了错误使用凌空库将数据发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有用凌空库了。我已阅读教程。我想将数据发送到一个URL这将在数据库中输入的数据。我曾尝试以下code,但它不工作。数据没有输入到数据库中。

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

任何人都可以请帮助我。 IAM的新本

can anyone please help me. iam new to this

推荐答案

,你应该使用 getBody 你的POST请求。

您可以参考我下面的工作示例code(取代我的的JSONObject URL由你和)。希望这有助于!

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

这篇关于错误使用凌空库将数据发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆