gpt4 book ai didi

使用 Node.js 时出现 Android-volley 发布错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:30:49 26 4
gpt4 key购买 nike

我知道这个问题可能已被问过多次,但似乎没有一个解决方案适合我。在我的 android volley post 请求中,我传递了用户名和密码,但每次我单击登录时,它总是说“无效的用户名和密码”,即使凭据是正确的并且它与 postman 一起工作得很好。这是我的代码如下:

登录 Node.js

router.post('/login', function(req,res,next){
var user = {
username: req.body.username,
password: req.body.password
};

connection.query("SELECT id, fullname, username, email_address, createdAt FROM users WHERE username=? OR email_address=? AND password=? LIMIT 1",[user.username, user.username, user.password], function(err, rows, field){
if(err) throw err;
if(rows.length > 0){
res.json({
success: '1',
message: 'Login Successful',
id: rows[0],
fullname: rows[1],
username: rows[2],
email: rows[3],
createdAt: rows[4]
});
}else{
res.json({
success: '0',
message: 'Invalid username or password'
});
}
});
});

客户端登录(android):

private void login(final String uname, final String upass) {
//192.168.1.101:5000/api/users/1
final String url = "http://192.168.1.100:5000/api/users/login";
RequestQueue queue = Volley.newRequestQueue(getActivity());

JsonObjectRequest rq = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if(response.getString("success") != null) {
final int success = Integer.parseInt(response.getString("success"));
if (success == 1) {
JSONObject uid = response.getJSONObject("id");
int id = uid.getInt("id");
String fullname = uid.getString("fullname");
String username = uid.getString("username");
String email = uid.getString("email");
String createdAt = uid.getString("createdAt");
SuperToast successToast = new SuperToast(getActivity(), Style.getStyle(Style.BLUE, SuperToast.Animations.FLYIN));
//successToast.setText(id + " " + fullname + " " + username + " " + email + " " + createdAt);
successToast.setText(response.getString("message"));
successToast.setDuration(SuperToast.Duration.LONG);
successToast.setGravity(Gravity.CENTER, 0, 0);
successToast.show();
} else if (success == 0) {
SuperToast errorToast = new SuperToast(getActivity(), Style.getStyle(Style.BLUE, SuperToast.Animations.FLYIN));
errorToast.setText(response.getString("message"));
errorToast.setDuration(SuperToast.Duration.MEDIUM);
errorToast.setGravity(Gravity.CENTER, 0, 0);
errorToast.show();
} else {
SuperToast errorToast = new SuperToast(getActivity(), Style.getStyle(Style.BLUE, SuperToast.Animations.FLYIN));
errorToast.setText("Invalid Request");
errorToast.setDuration(SuperToast.Duration.MEDIUM);
errorToast.setGravity(Gravity.CENTER, 0, 0);
errorToast.show();
}
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
SuperToast.create(getActivity(), error.getMessage(), SuperToast.Duration.LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}

@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("username", uname);
params.put("password", upass);
return params;
}
};
//MySingleton.getInctance(getActivity().getApplicationContext()).addToRequestQueue(rq);
queue.add(rq);
}

任何帮助将不胜感激,提前谢谢您。

最佳答案

您没有在请求正文中发送用户名和密码:

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

创建正文并设置请求:

JSONObject body = new JSONObject();
body.put("username", uname);
body.put("password", upass);

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

关于使用 Node.js 时出现 Android-volley 发布错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38236197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com