【问题标题】:Android - Volley request parameters not included in request?Android - 请求中不包含 Volley 请求参数?
【发布时间】: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


【解决方案1】:

感谢@jamesw 为我指明了正确的方向here。出于某种原因,此 POST 请求中未调用 getParams。我不知道为什么,但显然其他人也有同样的问题。

一种解决方法是创建一个 JSONObject 并将其传递给JsonObjectRequest

JSONObject parentData = new JSONObject();
JSONObject childData = new JSONObject();
  try {
    childData.put("email", mUserEmail);
    childData.put("password", mUserPassword);
    parentData.put("user", childData);
  } catch (JSONException e) {
    e.printStackTrace();
  }

将其传递给 JOR 构造函数:

JsonObjectRequest loginRequest = new JsonObjectRequest(Request.Method.POST, url, parentData, Listener..., ErrorListener...

对我有用,但如果有人可以解释如何致电 getParams() 或更清洁的解决方案,请随时回答,我会接受。

【讨论】:

  • 你应该使用StringRequest而不是JsonObjectRequest,看我的回答here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多