0

Hi i am trying to POST some data to PhP server and receive Json respond. But I still don't know how to POST different data type (ex String and int). Here is my code. Any help is much appreciate. Thanks (My staffID is String type, my beaconMajor and BeaconMinor are int type)

private void getEventRespondTest (RequestQueue requestQueue) {
        //Creating a string request
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,Config.DATA_URL,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject respond) {
                            try {
                                eventArray = new JSONArray();
                                eventDetail = new ArrayList<>();
                                eventArray = respond.getJSONArray("result");
                                eventDetail = getEventDetail(eventArray);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
//                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                            Toast.makeText(Beacon_MainActivity.this, "Unable to fetch data: " +error.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    }
                ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put(Config.STAFF_ID, staffID);
                map.put(Config.BEACON_MAJOR,beaconMajor);
                map.put(Config.BEACON_MINOR,beaconMinor);
                return map;
            }
        };

        //Adding request to the queue
        requestQueue.add(jsonObjectRequest);

    }

    private ArrayList getEventDetail(JSONArray j) {
        ArrayList event = new ArrayList();
        //Traversing through all the items in the json array
        for (int i = 0; i < j.length(); i++) {
            try {
                //Getting json object
                JSONObject json = j.getJSONObject(i);

                //Adding the name of the event to array list
                event.add(json.getString(Config.EVENT_TITLE));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        spinner.setAdapter(new ArrayAdapter<String>(Beacon_MainActivity.this, android.R.layout.simple_spinner_dropdown_item, event));
        return event;
    }
Lê Khánh Vinh
  • 2,591
  • 5
  • 31
  • 77
  • 1
    have a look at this: http://stackoverflow.com/questions/33833589/volley-request-with-raw-data/33975698#33975698 – TommySM Dec 02 '15 at 12:53
  • thanks for your respond. that help a lot. but how to handle the data on php server? i mean after sending array, how to decode and store to php array? – Lê Khánh Vinh Dec 02 '15 at 15:26

0 Answers0