Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Node.js时出现Android截击post错误_Android_Node.js_Android Volley - Fatal编程技术网

使用Node.js时出现Android截击post错误

使用Node.js时出现Android截击post错误,android,node.js,android-volley,Android,Node.js,Android Volley,我知道这个问题可能被问了很多次,但似乎没有一个解决方案适合我。在我的android截击post请求中,我传入用户名和密码,但每次单击“登录”时,它都会显示“无效用户名和密码”,即使凭据是正确的,邮递员也可以使用。下面是我的代码: 登录Node.js router.post('/login', function(req,res,next){ var user = { username: req.body.username, password: req.body.password

我知道这个问题可能被问了很多次,但似乎没有一个解决方案适合我。在我的android截击post请求中,我传入用户名和密码,但每次单击“登录”时,它都会显示“无效用户名和密码”,即使凭据是正确的,邮递员也可以使用。下面是我的代码:

登录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登录(最终字符串uname,最终字符串upass){
//192.168.1.101:5000/api/users/1
最终字符串url=”http://192.168.1.100:5000/api/users/login";
RequestQueue=Volley.newRequestQueue(getActivity());
JsonObjectRequest rq=新的JsonObjectRequest(Request.Method.POST,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
if(response.getString(“success”)!=null){
final int success=Integer.parseInt(response.getString(“success”);
如果(成功==1){
JSONObject uid=response.getJSONObject(“id”);
int id=uid.getInt(“id”);
String fullname=uid.getString(“fullname”);
字符串username=uid.getString(“用户名”);
String email=uid.getString(“电子邮件”);
String createdAt=uid.getString(“createdAt”);
SuperToast successToast=新建SuperToast(getActivity(),Style.getStyle(Style.BLUE,SuperToast.Animations.FLYIN));
//successToast.setText(id+“”+fullname+“”+username+“”+email+“”+createdAt);
successToast.setText(response.getString(“message”));
successToast.setDuration(supertoos.Duration.LONG);
successToast.setGravity(Gravity.CENTER,0,0);
successToast.show();
}else if(成功==0){
SuperToast errorToast=新建SuperToast(getActivity(),Style.getStyle(Style.BLUE,SuperToast.Animations.FLYIN));
errorToast.setText(response.getString(“消息”));
errorToast.setDuration(supertoos.Duration.MEDIUM);
errorToast.setGravity(重心,0,0);
errorToast.show();
}否则{
SuperToast errorToast=新建SuperToast(getActivity(),Style.getStyle(Style.BLUE,SuperToast.Animations.FLYIN));
errorToast.setText(“无效请求”);
errorToast.setDuration(supertoos.Duration.MEDIUM);
errorToast.setGravity(重心,0,0);
errorToast.show();
}
}
}捕获(JSONException ex){
例如printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
创建(getActivity(),error.getMessage(),supertoos.Duration.LONG).show();
}
}) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
返回标题;
}
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“用户名”,uname);
参数put(“密码”,upass);
返回参数;
}
};
//getInctance(getActivity().getApplicationContext()).addToRequestQueue(rq);
queue.add(rq);
}

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

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

JsonObjectRequest rq = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
JsonObjectRequest rq=newjsonobjectrequest(Request.Method.POST,url,null,new Response.Listener()){
创建正文并根据请求设置:

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>() {
JSONObject body=new JSONObject();
body.put(“用户名”,uname);
body.put(“密码”,upass);
JsonObjectRequest rq=新的JsonObjectRequest(Request.Method.POST、url、body、new Response.Listener()){

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

JsonObjectRequest rq = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
JsonObjectRequest rq=newjsonobjectrequest(Request.Method.POST,url,null,new Response.Listener()){
创建正文并根据请求设置:

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>() {
JSONObject body=new JSONObject();
body.put(“用户名”,uname);
body.put(“密码”,upass);
JsonObjectRequest rq=新的JsonObjectRequest(Request.Method.POST、url、body、new Response.Listener()){

我已经修复了,原来是电子邮件响应为空并引发异常,多亏了Tiago Ribeiro,代码终于可以工作了,下面是更新的代码

Login.js:

    router.post('/login', function(req,res,next){
    function encrypt(text){
        var cipher =  crypto.createHash('sha1')
            .update(text)
            .digest('hex');
        return cipher;
    }
    var uPassword = encrypt(req.body.password.toString());
  var user = {
    username: req.body.username,
    password: uPassword
  };

  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_address: 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());
        JSONObject params = new JSONObject();
        try {
            params.put("username", uname);
            params.put("password", upass);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest rq = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    final int success = response.getInt("success");
                      Log.d("Response", String.valueOf(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_address");
                            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();
                            Log.d("Response", response.toString());
                        } 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);
    }
private void登录(最终字符串uname,最终字符串upass){
//192.168.1.101:5000/api/users/1
最终字符串url=”http://192.168.1.100:5000/api/users/login";
RequestQueue=Volley.newRequestQueue(getActivity());
JSONObject参数=新的JSONObject();
试一试{
参数put(“用户名”,uname);
参数put(“密码”,upass);
}捕获(JSONException e){
e、 printStackTrace();
}
JsonObjectRequest rq=新的JsonObjectRequest(Request.Method.POST,url,params,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
final int success=response.getInt(“success”);
Log.d(“响应”,String.valueOf(成功));
如果(成功==1){
JSONObject uid=response.getJSONObject(“id”);
int id=uid.getInt(“id”);