gpt4 book ai didi

android - 检查登录Android的用户详细信息

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:10:45 26 4
gpt4 key购买 nike

我正在尝试为我的应用程序创建一个登录系统。目前用户可以在线创建一个帐户并下载该应用程序。然后系统会提示他们输入用户名和密码。

当他们按下登录按钮时,我想向服务器上的 php 脚本发出请求以检查结果,如果用户存在则返回 true,如果用户不存在则返回 false。

我对如何实现它有点困惑?

我正在尝试创建一个单独的类来扩展 AsyncTask

这是我的主 Activity

EditText username;
EditText password;
Button loginBtn;
LinearLayout loginform;
String passwordDetail;
String usernameDetail;
String url = "http://www.jdiadt.com/example/checklogindetails.php";

HttpTask httptask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Hide the Action Bar
ActionBar ab;
ab = this.getActionBar();
ab.hide();

//Get references to XML
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
loginBtn = (Button)findViewById(R.id.loginBtn);
loginform = (LinearLayout)findViewById(R.id.loginform);

//Animation
final AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f );
AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ;
fadeIn.setDuration(1200);
fadeIn.setFillAfter(true);
fadeOut.setDuration(1200);
fadeOut.setFillAfter(true);
fadeOut.setStartOffset(4200+fadeIn.getStartOffset());

//Run thread after 2 seconds to start Animation
Handler handler = new Handler();
handler.postDelayed(new Runnable(){

public void run() {
//display login form
loginform.startAnimation(fadeIn);
loginBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//display();
Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show();
if(checkLoginDetails()){
//OPENS NEW ACTIVITY
//Close splash screen
finish();
//start home screen
Intent intent = new Intent(v.getContext(), SectionsActivity.class);
startActivity(intent);
//creates fade in animation between two activities
overridePendingTransition(R.anim.fade_in, R.anim.splash_fade_out);
}
else{
}
}
});

}

}, 2000);


}

//Check the login details before proceeding.
public boolean checkLoginDetails(){
usernameDetail = username.getText().toString();
passwordDetail = password.getText().toString();
httptask = new HttpTask();
httptask.execute(url, usernameDetail, passwordDetail);
//if exists return true
//else return false
return false;
}

这是我的 HttpTask

public class HttpTask extends AsyncTask<String, Void, Boolean> {

@Override
protected Boolean doInBackground(String... params) {

String url = params[0];
String username = params[1];
String password = params[2];

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

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

try {
httpClient.execute(httpPost);
return true;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

这是我网络服务器上的 php 脚本 checklogindetails.php

    require_once 'db_connect.php';

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

$pwdMD5 = md5($password);

$sql = "SELECT * FROM users WHERE username = '$username' AND password='$pwdMD5'";
$result = mysql_query($sql);

$count = mysql_num_rows($result);

if($count == 1){
echo "Log in successful";
//RETURN TRUE
}
else{
echo "Wrong username or password";
//RETURN FALSE
}

我想我最困惑的地方是如何构建 php 脚本来检查登录详细信息,以及如何根据它返回 true 或 false 来决定要做什么。

我将不胜感激有关此主题的任何建议或帮助!非常感谢

最佳答案

除了您缺少最后一步之外,上面的代码看起来不错。从 PHP 返回一些东西,然后在应用程序中读取它。

我建议将 PHP 的输出更改为更易于解析/维护的内容,例如“OK”和“ERROR”

然后将以下代码添加到HttpTask。

final HttpResponse response = httpClient.execute(httpPost, localContext);
if (response != null)
{
// parse response
final HttpEntity entity = response.getEntity();
if (entity == null)
{
// response is empty, this seems an error in your use case
if (BuildConfig.DEBUG)
{
Log.d(HttpClient.TAG, "Response has no body"); //$NON-NLS-1$
}
}
else
{
try
{
// convert response to string
this.mResponseAsString = EntityUtils.toString(entity);
if (BuildConfig.DEBUG)
{
Log.d(HttpClient.TAG, "Response: " + this.mResponseAsString); //$NON-NLS-1$
}

// parse the string (assuming OK and ERROR as possible responses)
if (this.mResponseAsString != null && this.mResponseAsString.equals("OK")
{
// add happy path code here
}
else
{
// add sad path here
}
}
catch (final ParseException e)
{
Log.e(HttpClient.TAG, e.getMessage(), e);
}
catch (final IOException e)
{
Log.e(HttpClient.TAG, e.getMessage(), e);
}
}
this.mResponseCode = response.getStatusLine().getStatusCode();
}

就我个人而言,我还会将 HttpTask 中的“OK”重构为常量(以便于阅读和维护),并将大部分基于 HTTP 的代码重构为某种基类或实用程序类,以便您可以重用它。

关于android - 检查登录Android的用户详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14992919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com