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"];
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.
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);