【发布时间】:2014-06-23 23:25:20
【问题描述】:
我有一个JSONObjectRequest 我正在尝试通过 Volley 发送到我的 Rails 应用程序。我正在使用我的 Rails API,但收到 401 响应。我的 API 肯定是通过 curl 工作的,所以我认为我的 Volley 请求还没有完全正确。
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
}
};
编辑
带有 401 的 Rails 日志:似乎我的参数未包含在请求中。我在 Volley 请求中做错了什么,不包括在内?
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
【问题讨论】:
-
你能发布 Rails 的 401 错误日志吗
-
是的,使用 Heroku 的 Rails 日志进行了更新。
-
不知道。我只能建议您添加一些 log.d 消息来询问 android 应用程序中的流程和值。确保您认为正在发生的事情实际上就是正在发生的事情。
-
我注销了我的变量以确保它们被分配,它们是。我想我已经把它缩小到 getParams 方法。这可能是问题吗(第二个/第三个答案有意义,尽管不确定 Rails 是否像 PHP 一样处理这个问题)?stackoverflow.com/questions/19760212/…
-
getParams() 方法真的被调用了吗?
标签: android ruby-on-rails android-volley