【发布时间】: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