【问题标题】:Send POST request of Json Array as data to server using volley使用 volley 将 Json Array 的 POST 请求作为数据发送到服务器
【发布时间】:2023-03-15 00:16:02
【问题描述】:

我想使用 volley 发送帖子请求。我有这种格式的数据。

  [{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"10:29:17","id":1},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"15:59:41","id":2},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"16:05:53","id":3},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"16:06:16","id":4},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"16:06:51","id":5},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"16:08:36","id":6},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"16:13:33","id":7},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"16:22:32","id":8},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"17:00:46","id":9},{"date":"24 Jul 2015","doctorEmail":"mohd.aquib09@gmail.com","doctorName":"aquib","time":"17:04:04","id":10}]

我收到了这种格式的回复

{"Message" : "Success"}

我已经写了这个代码来发送请求。

   public void saveDataToServer(String data){

        final String BASE_URL = "http://spirantcommunication.com/andriod/grl/doctor_visit_track1.php";
        final String DOCTOR_JSON_PARAM = "doctorJson";

        HashMap<String, String> params = new HashMap<>();
        params.put(DOCTOR_JSON_PARAM, data);

        JsonObjectRequest request = new JsonObjectRequest(BASE_URL, new JSONObject(params),

                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try{
                            VolleyLog.v("Response:%n %s", response.toString(4));
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        MySingleton.getInstance(this).addToRequestQueue(request);
    }

但我无法发送它。请有人帮助我。

【问题讨论】:

  • “我无法发送它”,您尝试瘦身时会发生什么?
  • @RemeesMSyde 在日志猫中我没有得到任何回应..
  • 这里有一个解决方案,这可能对你有帮助stackoverflow.com/questions/28656865/…
  • 您好,您得到这个问题的解决方案了吗?

标签: android json android-volley


【解决方案1】:

为了使用 volley 发送一些 post 参数,您必须覆盖 getParams() 方法,该方法应该返回要以键值格式发送的参数列表。

请按照以下代码段向服务器发送正确的发布参数。

// Tag used to cancel the request
String tag_json_obj = "json_obj_req";

                  JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
                url, null,
                new Response.Listener<JSONObject>() {

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

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

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(DOCTOR_JSON_PARAM, data);                 

                return params;
            }

        };

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多