只有在验证用户名和密码正确后,如何在后台任务中启动活动?
Posted
技术标签:
【中文标题】只有在验证用户名和密码正确后,如何在后台任务中启动活动?【英文标题】:How to start an activity in BackgroundTask only after verifing that the username and password is correct? 【发布时间】:2016-10-31 09:04:25 【问题描述】:我正在尝试启动一个“显示”活动,该活动应该仅在用户通过身份验证后才启动(用户名和密码存储在 phpmyadmin 数据库中)
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class PatLogin extends Activity
EditText a,b;
String login_name,login_pass;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.pat_login);
public void patbuttonClick(View v)
if (v.getId() == R.id.patlogin)
a = (EditText) findViewById(R.id.TFpusername);
b = (EditText) findViewById(R.id.TFppassword);
login_name = a.getText().toString();
login_pass = b.getText().toString();
String method = "login";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method,login_name,login_pass);
//If possible I would like to call the "Display" activity from here but only when the correct username and password is entered.
//If it's not possible to call from here then I would like to know how to call "Display" activity from "BackgrounTask.java".
BackgroundTask.java 用于通过 phpmyadmin 数据库对用户进行身份验证(检查用户名和密码是否匹配)。
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundTask extends AsyncTask<String,Void,String>
AlertDialog alertDialog;
Context ctx;
BackgroundTask(Context ctx)
this.ctx =ctx;
int flag=0;
@Override
protected void onPreExecute()
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information....");
@Override
protected String doInBackground(String... params)
String login_url = "http://10.0.2.2/mobidoc/login.php";
String method = params[0];
if(method.equals("login"))
String login_name = params[1];
String login_pass = params[2];
try
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
response+= line;
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
return null;
@Override
protected void onProgressUpdate(Void... values)
super.onProgressUpdate(values);
@Override
protected void onPostExecute(String result)
if(result.equals("Registration Success..."))
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
else
alertDialog.setMessage(result);
alertDialog.show();
//When I use the following 2 lines of code, It calls the Display activity even if the wrong password is entered. I need a certain condition to be applied.
Intent myIntent = new Intent(ctx, Display.class);
ctx.startActivity(myIntent);
所以我想做的是调用一个名为“显示”的活动。仅当输入正确的用户名和密码时才应调用此活动。
我也附上php文件,仅供参考。
<?php
require "init.php";
$username = $_POST["login_name"];
$password = $_POST["login_pass"];
$password = md5($password);
$sql_query = "select name from pat_info where username like '$username' and password like '$password';";
$result = mysqli_query($con,$sql_query);
if(mysqli_num_rows($result)>0)
$row = mysqli_fetch_assoc($result);
$name = $row["name"];
echo "Login Success... Welcome ".$name;
else
echo "Login Failed...Try Again.";
【问题讨论】:
我建议您“无论如何都要开始活动”。但该活动的首要任务应该是由 it 确定是否允许其继续进行。 (为此,它咨询后台任务,并等待响应。) 如果未授权,则活动终止,如果授权则继续显示。启动活动的代码没有理由关心事情的结果。 . . 我应该怎么做。我对此完全陌生。 【参考方案1】:好的,我找到了解决方案。我必须比较 php 文件的回声并将其与存储“result”值的“res”进行比较。修改后的“BackgroundTask.java”如下:
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundTask extends AsyncTask<String,Void,String>
AlertDialog alertDialog;
Context ctx;
String res;
BackgroundTask(Context ctx)
this.ctx =ctx;
int flag=0;
@Override
protected void onPreExecute()
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information....");
@Override
protected String doInBackground(String... params)
String login_url = "http://10.0.2.2/mobidoc/login.php";
String method = params[0];
if(method.equals("login"))
String login_name = params[1];
String login_pass = params[2];
try
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
response+= line;
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
//I stored the response in the following String variable (res)
res = response;
return response;
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
return null;
@Override
protected void onProgressUpdate(Void... values)
super.onProgressUpdate(values);
@Override
protected void onPostExecute(String result)
if(result.equals("Registration Success..."))
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
else if(res.equals("Login Failed...Try Again."))
alertDialog.setMessage(result);
alertDialog.show();
else
alertDialog.setMessage(result);
alertDialog.show();
Intent myIntent = new Intent(ctx, Display.class);
ctx.startActivity(myIntent);
【讨论】:
以上是关于只有在验证用户名和密码正确后,如何在后台任务中启动活动?的主要内容,如果未能解决你的问题,请参考以下文章