仅对网站上的注册用户进行登录验证

时间:2012-11-15 05:54:05

标签: android authentication

我是Android应用程序的新手。我做了一个登录页面,只有已经注册的网站用户才能登录。当用户输入他的用户名和密码时,我想验证注册用户。如何验证用户名和密码。我使用以下代码请看这个并帮助我。单击登录按钮时,会显示一些错误。

Main.java

    public class Main extends Activity implements OnClickListener {

       Button ok,back,exit;
      TextView result;



     @Override
        public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   // Login button clicked
   ok = (Button)findViewById(R.id.btn_login);
   ok.setOnClickListener(this);

   result = (TextView)findViewById(R.id.lbl_result);

          }

         public void postLoginData() {
       // Create a new HttpClient and Post Header
       HttpClient httpclient = new DefaultHttpClient();

   /* login.php returns true if username and password is equal to saranga */
   HttpPost httppost = new HttpPost("http://yoursite.com/wp-login.php");

   try {
       // Add user name and password
    EditText uname = (EditText)findViewById(R.id.txt_username);
    String username = uname.getText().toString();

    EditText pword = (EditText)findViewById(R.id.txt_password);
    String password = pword.getText().toString();

       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("username", username));
       nameValuePairs.add(new BasicNameValuePair("password", password));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       // Execute HTTP Post Request
       Log.w("", "Execute HTTP Post Request");
       HttpResponse response = httpclient.execute(httppost);

       String str = inputStreamToString(response.getEntity().getContent()).toString();
       Log.w("", str);

       if(str.toString().equalsIgnoreCase("true"))
       {
        Log.w("", "TRUE");
        result.setText("Login successful");  
       }else
       {
        Log.w("", "FALSE");
        result.setText(str);            
       }

   } catch (ClientProtocolException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
    }

    private StringBuilder inputStreamToString(InputStream is) {
          String line = "";
           StringBuilder total = new StringBuilder();
           // Wrap a BufferedReader around the InputStream
             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                    // Read response until the end
                  try {
               while ((line = rd.readLine()) != null) {
               total.append(line);
                }
              } catch (IOException e) {
              e.printStackTrace();
                       }
                // Return full string
             return total;
              }

              public void onClick(View view) {
                if(view == ok){
                 postLoginData();
                 }
                 }

                }

错误

 Execute HTTP Post Request
   Default buffer size used in BufferedReader constructor. It would be better to be       explicit if an 8k-char buffer is required.
  <html><head>    <title>yoursite.com: The Leading Website Site on the    Net</title>            <script type="text/javascript">;        if(self != top)    top.location.href = 'http://'+location.hostname+'/?redir=frame&  uid=yoursite50a47fe3380989.09080642';        </script>        <script   type="text/javascript" src="http://return.bs.domainnamesales.com  /return_js.php?d=yoursite.com&s=1352957923"></script>    </head>    <frameset cols="1,*,1"    border=0>        <frame name="top"   src="tg.php?uid=yoursite50a47fe3380989.09080642" scrolling=no frameborder=0 noresize   framespacing=0 marginwidth=0 marginheight=0>        <frame   src="search.php?uid=yoursite50a47fe3380989.09080642" scrolling="auto" framespacing=0  marginwidth=0 marginheight=0 noresize>        <frame   src="page.php?yoursite50a47fe3380989.09080642"></frame>    </frameset>      <noframes>        yoursite.com has been connecting our visitors with providers of Computer  Networking, Dedicated Web Servers, Email Domain Hosting and many other related  services for nearly 10 years. Join thousands of satisfied visitors who found Ftp Hosts, Ftp Servers, Host, Internet Servers, and Linux Servers.<br/>    </noframes></html>
FALSE

2 个答案:

答案 0 :(得分:1)

试试这个例子。

http post method passing null values to the server

在这里你必须发送用户名&amp;密码到Web服务器,您将根据请求从服务器获得响应。因此,如果使用是否注册,您必须在服务方面进行检查。

我已使用JSON来检索回复。

答案 1 :(得分:0)

如果你真正正确地检查了logcat,你会发现你已经达到了你想要达到的目标。

你尝试了什么: 发送HTTP POST请求以进行登录。 你得到的回应是什么: HTML页面。

对于该特定HTTP POST请求,您的webapp不会返回true / false。而您正在检查 true

解决方案:
1.解析HTML页面以确定是否成功登录(不推荐)
2.重新设计您的Web应用程序以返回xml / json响应。您可以同时拥有可以在任何其他应用程序中使用的网页和API。

你应该注意的其他事项:
1.永远不要在UI线程上进行网络调用。可能最终得到 ANR
2.永远不要离开没有标签的日志语句。标签使调试更容易。当你编写大量代码并处理很多复杂问题时,你会理解这一点。

希望这有帮助。