【问题标题】:Volley not attaching parameters to my url via POST request with StringRequestVolley 没有通过使用 StringRequest 的 POST 请求将参数附加到我的 url
【发布时间】:2018-05-01 02:00:00
【问题描述】:

我正在尝试使用 volley 将分数添加到我的数据库中。我已经使用 Postman 使用 POST 请求测试了我的 URL,我还在 android 应用程序中手动输入了带有参数的 URL 到 POST 请求,并且可以正常工作。所以我认为我的问题出在我的 android 代码中?还是我必须使用 getParams 手动构建 url 字符串?

我从后端得到的错误: 错误:ER_BAD_FIELD_ERROR:“字段列表”中的未知列“未定义”

这让我觉得我的参数在调用 StringRequest 之前没有被初始化。

public void submitHighScore(){
    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    // response
                    Toast.makeText(context, response , Toast.LENGTH_SHORT).show();
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Toast.makeText(context, "Error: unable to submit Score " + error , Toast.LENGTH_SHORT).show();
                }
            }
    ){@Override
        protected Map<String, String> getParams() throws com.android.volley.AuthFailureError
        {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("playerName", "Android test");
            params.put("playerScore", "3000");

            return params;
        }
    };

    HighScoreSingleton.getInstance(context).addToRequestQueue(postRequest);

对于那些可能相信或不相信我的服务器端代码的人。

app.post('/api/highScore', function(req,res){
    var con = mysql.createConnection({
        host: "xxxx",
        user: "xxxx",
        password: "xxxx",
        database: "xxxx"
    });
    if(req.query!=null){ //
        console.log(typeof req.query.playerName);
    con.query(`INSERT INTO xxxx (playerScore, playerName) VALUES
            (${req.query.playerScore}, "${req.query.playerName}" )`, // async, runs callback aka function
         function(err,rows){
            if(err) throw err;
            res.setHeader('Content-Type', 'text/plain');
            res.send('Updated High Score');
        });

    con.end();
    }
});

提前谢谢你,很可能我忽略了一些非常简单的事情,否则我将在我的 StringRequest 中修改我的 URL,因为这看起来有点傻,考虑到 getParams 方法。

【问题讨论】:

    标签: android parameters http-post android-volley


    【解决方案1】:

    我希望这对你有用。

    我认为你需要添加标题

    重写此方法。

    @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params =  super.getHeaders();
            if(params==null)params = new HashMap<>();
            params.put("Content-Type","text/plain");
            return params;
        }
    

    【讨论】:

    • 感谢您的意见。由于我是后端开发的新手,我误以为尝试使用 getParams() 方法通过查询字符串连接到 url,而不是使用实际存储参数的消息正文。
    猜你喜欢
    • 2018-06-16
    • 2020-03-18
    • 2016-05-04
    • 2017-09-12
    • 2018-04-01
    • 2022-01-04
    • 2020-11-20
    • 1970-01-01
    • 2022-10-06
    相关资源
    最近更新 更多