0

我已经设法使工作成为登录屏幕,从这里获取 java 脚本在此处 输入链接描述, 但我的问题是,即使登录错误,我请求的新活动也会打开。

这是代码,

package com.xxxxxx;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Admin extends Activity {
    /** Called when the activity is first created. */
    private Button login;
    private EditText username, password;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin);

        login = (Button) findViewById(R.id.login);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String   mUsername = username.getText().toString();
                String  mPassword = password.getText().toString();

                tryLogin(mUsername, mPassword);
            }
        });
    }

    protected void tryLogin(String mUsername, String mPassword)
    {           
        HttpURLConnection connection;
       OutputStreamWriter request = null;

            URL url = null;   
            String response = null;         
            String parameters = "username="+mUsername+"&password="+mPassword;   

            try
            {
                url = new URL("http://xxxxxxxxxxxxxxx/login.php");
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestMethod("POST");    

                request = new OutputStreamWriter(connection.getOutputStream());
                request.write(parameters);
                request.flush();
                request.close();            
                String line = "";               
                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                BufferedReader reader = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                // Response from server after login process will be stored in response variable.                
                response = sb.toString();
                // You can perform UI operations here
                Toast.makeText(this,"Message from Server: \n"+ response, 0).show();             
                isr.close();
                reader.close();
            Intent intent = new Intent (Admin.this, Webview.class);
            startActivity(intent);
            finish();
            }
            catch(IOException e)
            {
                // Error
            }
    }
}

我猜 startactivity 有问题,但我无法修复它。提前致谢。

这是php

<?php

$username = $_POST['username'];
$password = $_POST['password'];

if($username=='xxxxxxx' && $password == 'xxxxxxxx'){
echo "true";
}
else{
echo "Login Failed";
}

?>
4

3 回答 3

1

如果登录不成功,http 请求不会抛出错误。您很可能会从服务器取回 401 代码。您需要在逻辑中处理来自服务器的代码。此外,您可能希望您的 tryLogin 方法只返回 true 或 false,然后有一个 if 语句来启动或不启动 Activity。

于 2011-09-19T18:08:31.900 回答
1

没有伙伴...它将始终启动您在此处提到的活动,除非在尝试从服务器获取响应时发生任何异常...现在发生的事情即使登录,您也会从服务器获得某种响应失败。您需要的只是在开始新活动之前有一个条件来检查登录是否成功...

例如:从服务器返回“1”表示登录成功,0 表示登录失败。

于 2011-09-19T18:09:20.887 回答
1

startActivity 调用似乎没有任何问题。startActivity 不知道您的登录是否正确,它被设置为始终触发,因此它每次都会这样做。

您可能想要检查登录尝试中的 HTTP 响应代码,或者解析登录尝试的输出以查看它是否成功。使用其中任何一种方法,当且仅当登录尝试成功时,您才可以选择调用 startActivity。

编辑:
根据您的代码,字符串响应应包含“true”或“Login Failed”。检查响应的内容,只有在响应为“true”时才调用 startActivity,也许在“登录失败”的情况下做其他事情来通知用户。

于 2011-09-19T18:07:55.217 回答