Luke Swain Luke Swain - 4 months ago 13x
PHP Question

How to get HashMap in PHP sent via Volley in PHP?

I am working on an android application and am trying to post 'username' and 'password' parameters to a server to be processed.

I have done this in Java using volley to send a hashMap to the server as I found out how to do via external research.

final HashMap<String, String> dataToSend = new HashMap<>(); //Contains key/value pairs of data we will send to the server
//Add the data to the data package
dataToSend.put("username", username); //Add username to the package
dataToSend.put("password", password); //Add password to the package
final String url = "url here"; //The url we will make the delivery to.

//Making the object request, parsing the data to send to the server
JsonObjectRequest loginRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(dataToSend), new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
try {
Toast.makeText(getContext(), "You said: " + response.getString("username"), Toast.LENGTH_SHORT).show();
catch (Exception e){
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
//An error occurred during transmission, log to console and notify user.
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();

RequestQueue loginRequestQueue = Volley.newRequestQueue(getContext());
loginRequestQueue.add(loginRequest); //Add the above request to the queue for execution.

I then have my PHP script on the server, called from volley via https in url.
At the moment all I need to do is get the username and password out of the hashMap without using GET for security reasons.

I have no idea how to do that and am unable to find any help online.
Here is PHP as it stands at the moment:

$username = $_POST["username"];
$response = json_encode(array('username' => $username));
echo $response;

The above always returns null? How would I get the data into the PHP script from the HashMap? I think the issue is with my PHP but I am unsure.


I think you should overide the below method in request like below

        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put(KEY_EMAIL, email);
            return params;