I have a JSONObjectRequest
I'm trying to send to my Rails app via Volley. I'm hitting my Rails API, but am getting 401 responses. My API definitely works via curl, so I think I haven't formed my Volley request quite right.
public void login(View button) {
EditText userEmailField = (EditText) findViewById(R.id.userEmail);
mUserEmail = userEmailField.getText().toString();
EditText userPasswordField = (EditText) findViewById(R.id.userPassword);
mUserPassword = userPasswordField.getText().toString();
if (mUserEmail.length() == 0 || mUserPassword.length() == 0) {
// input fields are empty
Toast.makeText(this, "Please complete all the fields",
Toast.LENGTH_LONG).show();
return;
} else {
JsonObjectRequest loginRequest = new JsonObjectRequest(Request.Method.POST, url, null,
new Response.Listener<JSONObject>() {
@Override public void onResponse(JSONObject response) {
try {
//everything is good
if (response.getBoolean("success")) {
SharedPreferences.Editor editor = mPreferences.edit();
//Save auth_token into shared preferences
editor.putString("AuthToken", response.getJSONObject("data").getString("auth_token"));
editor.commit();
// launch the HomeActivity and close this one
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
} catch (Exception e) {
// something went wrong: show Toast
//with the exception message
Toast.makeText(myActivity, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener()
{
@Override public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("email", mUserEmail);
params.put("password", mUserPassword);
return params;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(loginRequest); //Call to get dashboard feed
}
};
EDIT
Rails log with 401: It appears that my params aren't being included with the request. What did I do wrong in the Volley request that it wouldn't be included?
Started POST "/api/v1/sessions" for 11.111.111.11 at ....
Processing by Api::V1::SessionsController#create as JSON
Parameters: {"session"=>{}}
Completed 401 Unauthorized in 2ms