无法通过Android中的排球以url编码形式发布params

时间:2016-09-01 14:58:00

标签: android android-volley

我正在尝试点击api(用PHP编写)并用它发布params。 这是我的代码:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, null,
            new com.android.volley.Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    System.out.println("prerna succes volley "+response.toString());
                }
            }, new com.android.volley.Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            System.out.println("prerna fail volley "+error.toString());
        }
    })


    {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> pars = new HashMap<String, String>();
            pars.put("Content-Type", "application/x-www-form-urlencoded");
            return pars;
        }


        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("action", "login");
            params.put("username", "abc@xyz.com");
            params.put("pass", "a");
            return params;
        }

    };

我总是得到无效的用户名/密码,如果用户名和密码无效,已经在api中处理过。在这种情况下,api没有收到参数。 我试图通过改造来实现它并且它与它一起工作正常,这意味着API编码没有问题。如果有凌空抽射,我在这里错过了什么?

4 个答案:

答案 0 :(得分:4)

感谢您的支持。我能够通过将JsonObjectRequest更改为StringRequest来解决问题。我发现Volley JsonObjectRequest Post parameters no longer work我知道JsonObjectRequest会产生一些意想不到的问题。 有我的代码:

    StringRequest jsonObjReq = new StringRequest(Request.Method.POST,
            url,
            new com.android.volley.Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonObject = new JSONObject(response.toString());
                        String no = jsonObject.getString("$PhoneNo");

                    } catch (JSONException e) {

                    }
                }
            }, new com.android.volley.Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            System.out.println("prerna fail volley " + error.toString());
        }
    })


    {
        @Override
        public String getBodyContentType() {
            Map<String, String> pars = new HashMap<String, String>();
            pars.put("Content-Type", "application/x-www-form-urlencoded");
            //return pars;
            return "application/x-www-form-urlencoded";
        }


        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("action", "login");
            params.put("username", "abc@xyz.com");
            params.put("pass", "a");
            return params;
        }

    };

但是我仍在试图找出JsonobjectRequest无效的原因。

答案 1 :(得分:0)

据我所知,通过查看JsonObjectRequest您的第3个参数为null,它表示JsonObject请求 - 由文档&#34; jsonRequest - JSONObject发布请求。允许Null,表示不会随请求一起发布参数。&#34;

再次搜索后,我看到了this thread

答案 2 :(得分:0)

尝试迁移到getHeaders并移除getParams

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("action", "login");
        params.put("username", "abc@xyz.com");
        params.put("pass", "a");
        return pars;
}

或 - 重用getParams方法

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = getParams();
        params.put("Content-Type", "application/x-www-form-urlencoded");
        return pars;
}

如果是身体请求,您需要做的就是添加额外的参数。

JSONObject body = new JSONObject();
body.put("action", "login");
body.put("username", "abc@xyz.com");
body.put("pass", "a");

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, body,

            new com.android.volley.Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    System.out.println("prerna succes volley "+response.toString());
                }
            },
 new com.android.volley.Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            System.out.println("prerna fail volley "+error.toString());
        }
    });

答案 3 :(得分:0)

如果您要以原始格式发送数据,则需要尝试此操作,首先要使您的数据的json发布。

final JSONObject jsonBody = new JSONObject();
                jsonBody.put("mobile", phoneNumber);
                jsonBody.put("otp", otp.trim());

你必须覆盖这些方法

@覆盖                 public byte [] getBody()抛出AuthFailureError {                     return jsonBody.toString()。getBytes();                 }

            @Override
            public String getBodyContentType() {
                return "application/json";
            }

如果您的帖子请求中也有标题,您也可以通过以下类型

发送标题
  @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        Map<String, String> params = new HashMap<String, String>();
                        String auth_token = "bearer" + prefs.getPreferencesString(context, "AuthToken").toString();
                        params.put("Authorization", auth_token);
                        return params;
                    }

以下是整个代码。

 final JSONObject jsonBody = new JSONObject();
                jsonBody.put("mobile", phoneNumber);
                jsonBody.put("otp", otp.trim());

                StringRequest sr = new StringRequest(Request.Method.POST, Constraints.Base_URL + "/api/v1/verifyotp", new com.android.volley.Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            progressDialog.dismiss();
                            JSONObject jsonObject = new JSONObject(response);
                            int status = jsonObject.getInt("status");
                            String token = jsonObject.getString("token");
                            prefs.setPreferencesString(OtpActivity.this, "AuthToken", token);

                            if (status == 1) {

                                startActivity(new Intent(getApplicationContext(), WelcomeScreenActivity.class));
                                overridePendingTransition(R.anim.right_in, R.anim.left_out);
                            } else if (status == 0) {
                                Toast.makeText(OtpActivity.this, "Please Enter Otp!", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {

                        }
                    }
                }, new com.android.volley.Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Toast.makeText(OtpActivity.this, "Invalid otp please try again!!", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }
                }) {

                    @Override
                    public byte[] getBody() throws AuthFailureError {
                        return jsonBody.toString().getBytes();
                    }

                    @Override
                    public String getBodyContentType() {
                        return "application/json";
                    }
                };

                MyApplication.getInstance().addToReqQueue(sr, "jreq");
            }
        } catch (Exception e) {

        }