Perfect Circle Perfect Circle - 1 month ago 8
Android Question

Android Volley Request not sending POST data to server/database

I am developing an android app that I have been trying to incorporate a social media login, I am trying to add the details retrieved via the Social media login (name & email) and add it to a database of users on our server via a Volley request.
So far I have managed to get the users details succesfully and add it to the Shared Preferences using the 'session' class I created, however calling another method to send the data to the server does not work.

facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
String fbID = null;
String fbName = null;
String fbEmail = null;
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
fbID = object.getString("id");
fbName = object.getString("name");
fbEmail = object.getString("email");

session.setLogin(true);
session.setUserDetails(fbEmail,fbName);

} catch (JSONException e) {
e.printStackTrace();
} finally {
registerSocialUser(fbName, fbEmail);
}
}
});


As you can see in my try/catch I make a request to the registerSocialUser() method via a 'finally' block. This calls the following method fine and the Console log's the users details, however it is never added to the database.

private void registerSocialUser (final String name, final String email) {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, Config.REGISTER_SOCIAL_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("CREATION", "Social Media Account Added: " + name + " " + email);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("CREATION", "Create Error");
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("user",name); //name
params.put("email",email); //email
return params; //return the parameters

}
};
queue.add(request);
Log.d("CREATION", "SUCCESS");
}


The logcat returns:

Social Media Account Added: *My name* *My email*


The Config.REGISTER_SOCIAL_URL is a php file which takes two POST parameters (name and email) and adds them to the database after checking it doesn't already exist. I can call this manually through my browser with exactly the same details and it works fine and adds to the database.

Excuse me if any of my code is really bad practice, i'm pretty new to Android Development so would really appreciate the help :)

Answer

While sending parameters you can easily use POST request method. There are plenty of chances that you would have been missing some method.

    private void registerSocialUser (final String name, final String email) {
RequestQueue queue = Volley.newRequestQueue(this);
String stopArchiveUrl = Config.REGISTER_SOCIAL_URL;
StringRequest stringRequest = new StringRequest(Request.Method.POST, stopArchiveUrl,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("CREATION", "Social Media Account Added: " + name + " " + email);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("ERROR", "Error adding Social Media Account");
        }
}){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("name",name); //name
            params.put("email",email); //email
            return params; //return the parameters
        }
    };
// Add the request to the RequestQueue.
queue.add(stringRequest);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();

}

I think problem not lies in your code. It is the strings that you are passing in the parameters while using POST, you are using

                   params.put("user",name); //name

which should be

                    params.put("name",name); //name

Also it would be better if you attach a token using a secret key for validation on the server side too. So, that no one hit your url (if leaked somehow) anonymously.