在我的midlet中验证用户名和密码的问题

时间:2011-09-28 08:21:26

标签: java netbeans java-me svg

我修改了登录并改为实现了。

 public void login() {         

        HttpConnection c = null;
        InputStream is = null;
        StringBuffer str = new StringBuffer();

        String url = "http://101.00.00.000/...../"
                + "?user=" + getUsername().getString().trim()
                + "&password=" + getPass().getString().trim();
        try {
            c = (HttpConnection) Connector.open(url);
            is = c.openInputStream();

            int len = (int)c.getLength();
            int ch;
            String getS = "";
            while((ch = is.read()) != -1){
                str.append((char)ch);
                getS = str.toString();
                String msg = "Login successful";
                System.out.println(getS.substring(0, getS.length()));
                if (getS.substring(0, 16).equals(msg)){
                System.out.println(getS);
                switchDisplayable(null, svgMenu.getSvgCanvas());
            }else{
                switchDisplayable(null, getAlert2());
//            }
            }

            System.out.println(getS);



        } 
        catch (IOException exception) {
            try {
                if (is != null)
                  is.close();
              if (c != null)
                  c.close();
                exception.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } 
    }

网址对调试的响应给出了正确的答案,即登录成功,即在命令行中。但我注意到字符串比较没有返回正确的逻辑 即

if (getS.substring(0, 16).equals(msg)){
                    System.out.println(getS);
                    switchDisplayable(null, svgMenu.getSvgCanvas());
                }

if语句永远不会执行。我该怎么做。

1 个答案:

答案 0 :(得分:0)

你在服务器方面犯了一个错误。因为您从客户端(移动)发送用户名和密码的字符串值。你需要在服务器端验证。在此之前,您需要检查客户端(移动)上的用户名和密码是否有空格。使用getUsername().getString().trim()getPass().getString().trim()以及如果需要检查区分大小写。