0

How I can post a string to a PHP web server and get JSONArray in response using Volley library in Android. Following is my code in which I want response. How I can post a string and get JSONArray in response using this function

See this code if it works

JsonArrayRequest(Method.POST,Config.VIEW_PROFILE_URL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        //calling method to parse json array
                        parseData(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        // TODO Auto-generated method stub

                    }
                }){

            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("user_name", user);
                return params;
            }
        };
          MySingleton.getInstance(this).addToRequestQueue(jsonArrayRequest);
        }

//and receiving at server like

$username =$_POST["user_name"];

2 Answers2

1

Use getParams() method in your request to send parameter to server
you can also set a string as body after url:

  JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, "url", "you can send a string here as body", new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            // parse your json array 
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // handle errors here
        }
    }) {
        @Override
        protected Map<String, String> getParams() {   // send parameters here
            Map<String, String> params = new HashMap<>();
            params.put("key1", "value1");
            // add other parameters
            return params;
        }
    };
    MySingleton.getInstance(this).addToRequestQueue(request);
Ali Sherafat
  • 3,506
  • 2
  • 38
  • 51
  • I want to post a user name of string value having key "user_name" Tell me by using your method how I will post the string to php server and receive there in php? – Zeshan Masood Jul 19 '16 at 06:28
  • @ZeshanMasood just put your key value parameters in `getParams()` like i did and you can find a lot of example out there for getting parameters in php – Ali Sherafat Jul 19 '16 at 08:20
  • Then wht to do with this "you can send a string here as body" – Zeshan Masood Jul 19 '16 at 17:23
  • that is optional, if you just need to send parameter you can delete that body string! and don't forget to upvote if my answer helped you – Ali Sherafat Jul 19 '16 at 17:29
  • I have tried but it is not sending parameters to server – Zeshan Masood Jul 19 '16 at 18:31
  • i'm sure this code works maybe you miss something in server side! make sure you are getting parameters in php using `$_POST['key']` – Ali Sherafat Jul 19 '16 at 19:08
  • dear its not working, it works if I request a string in response but not in the case of JSONArray – Zeshan Masood Jul 20 '16 at 06:55
  • maybe you are sending a string from server and it's not a standard JsonArrray – Ali Sherafat Jul 20 '16 at 10:12
  • I have checked all the problem is that the string is not being received at server I am using correct form of key value pair – Zeshan Masood Jul 20 '16 at 12:41
  • tell me how I will receive the string on php server if I post a string using string body – Zeshan Masood Jul 21 '16 at 08:13
  • JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Method.POST,url, new Response.Listener() { @Override public void onResponse(JSONArray response) { //parse } }, new Response.ErrorListener() { public void onErrorResponse(VolleyError error) {Log.e(MainActivity.class.getSimpleName(), "Server Error: " + error.getMessage());} }) { protected Map getParams(){ Map params = new HashMap<(); params.put("user_name", user); return params; } } – Zeshan Masood Jul 21 '16 at 09:53
0

Follow this here link you can understand both parsing technique JSONArray and JSONObject .

Another thing is POST to server it's depend on your API , either using parameter and header or concat with URL with key-value by StringRequest.

Osman Goni Nahid
  • 1,193
  • 2
  • 15
  • 24