本文介绍了使用齐射将Json Array的POST请求作为数据发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用截击发送发帖请求.我有这种格式的数据.

  [{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"10:29:17","id":1},{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"15:59:41","id":2},{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"16:05:53","id":3},{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"16:06:16","id":4},{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"16:06:51","id":5},{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"16:08:36","id":6},{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"16:13:33","id":7},{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"16:22:32","id":8},{"date":"24 Jul 2015","doctorEmail":"[email protected]","doctorName":"aquib","time":"17:00:46","id":9},{"date":"24 Jul 2015","doctorEmail":"[email protected]","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);
    }

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

解决方案

为了使用截击发送一些后置参数,您必须重写getParams()方法,该方法应以键值格式返回要发送的参数列表. /p>

请按照下面的代码段向服务器发送适当的post参数.

// 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);

I want to send a post request using volley. I have data in this format.

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

And I am getting response in this format

{"Message" : "Success"}

I have written this code to send request.

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

But I am not able to send it.Please somebody help me.

解决方案

In order to send some post parameters using volley you have to override getParams() method which should return list of parameters to be send in a key value format.

Kindly follow below code segment for sending proper post parameters to server.

// 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);

这篇关于使用齐射将Json Array的POST请求作为数据发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:33