android - Volley 向服务器发送空参数

标签 android android-volley

我正在尝试发送 JSON 参数,但服务器将它们作为空值接收, 我试着从 Postman 那里请求它,它工作得很好,我不知道 volley 有什么问题我按照说明做了 here但这没有意义

这是我的代码

String url = "http://10.10.10.166:8080/SystemManagement/api/Profile/Login";
    JSONObject jsonObject=new JSONObject();

    try {
        jsonObject.put("user_id","Test user name");
        jsonObject.put("user_password","test password");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    System.out.println(jsonObject.toString());

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(  Request.Method.POST, url, jsonObject,
            new Response.Listener<JSONObject>() {



                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    System.out.println(error.toString());

                }
            });
    //add request to queue
    queue.add(jsonObjectRequest);

最佳答案

我遇到了同样的问题,通过覆盖 getParams() 方法解决了

这是我使用 Volley 的登录请求。

private void loginRequeset() {
            showpDialog();

            StringRequest jsonObjReq = new StringRequest(Request.Method.POST, Constants.LOGIN_URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
      hidepDialog();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    // hide the progress dialog
                    hidepDialog();
                }
            })

            {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> signup_param = new HashMap<String, String>();
                    signup_param.put(Constants.USERNAME, _emailText.getText().toString().trim());
                    signup_param.put(Constants.PASSWORD, _passwordText.getText().toString().trim());
                    return signup_param;
                }

            };

            // Adding request to request queue
            queue.getInstance().addToRequestQueue(jsonObjReq);
        }

关于android - Volley 向服务器发送空参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36181238/

相关文章:

Android - 检测我的应用程序的其他用户是否在附近

java - 为什么我的 java 服务器和 android 客户端无法来回发送任何消息?

android - 使用 volley 的 HTTPS 请求

安卓 Volley : Unexpected response code 405

android - 如何防止 ListView 在大小改变时重新加载?

java - 为什么签名为空?

安卓工作室 : NoClassDefFoundError on all classes of the app module

java - 等待 Async Volley 请求的结果并返回它

java - 使用 volley 从 php 页面发送和接收

java - EditTextPreference 不适用于字符串