2016-07-07 48 views
0

我知道這個問題可能已被多次詢問,但沒有一個解決方案似乎適用於我。在我的android volley post請求中,我傳遞了用戶名和密碼,但每次我單擊登錄時,它總是會說'無效的用戶名和密碼',即使憑據是正確的,並且郵遞員也能正常工作。在這裏我下面的代碼:使用Node.js時Android-volley發佈錯誤

登錄的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' 
     }); 
     } 
    }); 
}); 

登錄:

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); 
} 

任何幫助將非常感激,謝謝提前。

回答

0

我已經固定它,原來它是電子郵件的答覆是空和投擲例外,並感謝蒂亞戈裏貝羅代碼終於工作,這裏的更新代碼

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); 
    } 
2

你不是在發送請求正文中的用戶名和密碼:

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>() { 
+0

試過,沒有奏效 – codex

+0

有什麼反應? –

+0

它沒有給出任何迴應 – codex