【问题标题】:Android Volley Post RequestAndroid Volley 发布请求
【发布时间】:2016-10-27 07:56:50
【问题描述】:

我已经通过 volley 实现了 post 请求。但是它有一些奇怪的行为。我收到响应,因为服务器没有响应,但这些帖子数据仍然被插入数据库两次。 参数是从edittext字段中获取的简单字符串类型数据。对此有任何帮助。

    StringRequest stringRequest = new StringRequest(Request.Method.POST, GlobalConfig.ADD_ACTIVITY_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    Log.e("response", response);                               

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    GlobalCommonMethods.handleErrorResponse(ActivityTeacher.this,error,"Server not responding !!!");
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> arguments = new HashMap<String, String>();
            arguments.put("teacher_id", "ds";

            arguments.put("action", "comment");

            arguments.put("user_type", "teacher");
            return arguments;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

【问题讨论】:

  • 你能发布凌空错误吗?

标签: android android-volley


【解决方案1】:

我猜你在catch() {...} 之前缺少try {...}。下面是我的意思的一个例子:

StringRequest stringRequest = new StringRequest(Request.Method.POST, GlobalConfig.ADD_ACTIVITY_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {

                    Something here...

                     } catch (JSONException e) {

                    Something here ...

                     }
                }
            }, ...

【讨论】: