简体   繁体   中英

How to send JSON Array of objects using Volley in android

I want to send some bulk data to my php server so I constructed JSON Array. But how to send it using volley in Android. Could you anybody help. I already tried many ways but didnt work.

Below is my code for the dataset

            JSONArray jsData = new JSONArray();
            JSONObject others = new JSONObject();

                
                while(crsrallansr.isAfterLast() == false) {
                    JSONObject Inner = new JSONObject();
                    try {
                        Inner.put("qid",crsrallansr.getString(crsrallansr.getColumnIndex("qid")));
                        Inner.put("qstn",crsrallansr.getString(crsrallansr.getColumnIndex("qid")));
                        Inner.put("result",crsrallansr.getString(crsrallansr.getColumnIndex("qid")));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    jsData.put(Inner);
                   
                    crsrallansr.moveToNext();
                    xx++;
                }
                

Fixed the problem using StringRequest like :

reqPostanswers = new StringRequest(Request.Method.POST, url,new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.i("posting info :",response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Log.i("posting error  :",error.toString());
            }
        }){
            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("user", thisuser);
                params.put("answers",jsData.toString());
                params.put("lickey","1761");
                return params;

            }

        };
        answerpostQueue = Volley.newRequestQueue(getApplicationContext());
        answerpostQueue.add(reqPostanswers);

At the server side ( php ); the code is as follows :

        $answers=json_decode($_POST['answers']);
        
        foreach ($answers as $answer) {
            $answer=json_encode($answer);
            echo $answer;
            $answer=json_decode($answer);
            $uname=$_POST['user'];
            $qid=$answer->qid;
            $result=$answer->result;
            $qstn=$answer->qstn;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM