![](/img/trans.png)
[英]how to direct to another activity after successful login, Android Developer Studio
[英]How can I load the new activity after login in android studio?
基本上,我想在使用数据库登录后开始一个新活动。 我怎样才能做到这一点?
这是我的登录 class
public class login extends AppCompatActivity {
EditText username;
EditText password;
Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = username.getText().toString();
String pass = password.getText().toString();
String type = "login";
System.out.println("IN Onclick login");
BackgroundTask backgroundTask = new BackgroundTask(getApplicationContext());
backgroundTask.execute(type,user,pass);
if(backgroundTask.statusOK.equals("true")) {
Intent loginIntent = new Intent(login.this,loggedIn.class);
startActivity(loginIntent);
}
}
});
}
}
您可以在上面的代码中看到,我正在使用此代码在成功登录后开始我的新活动,但这并不是开始新活动。 我不知道为什么?
if(backgroundTask.statusOK.equals("true")) {
Intent loginIntent = new Intent(login.this,loggedIn.class);
startActivity(loginIntent);
}
这是后台任务 class
public class BackgroundTask extends AsyncTask<String, String, String> {
Context context;
public String statusOK="false";
BackgroundTask(Context ctx){
this.context = ctx;
}
@Override
protected String doInBackground(String... strings) {
System.out.println("In doin");
String type = strings[0];
String loginURL = "http://192.168.10.119/log/login.php";
String regURL = "http://192.168.10.119/log/log.php";
if(type.equals("reg")){
String name = strings[1];
String pass = strings[2];
try{
URL url = new URL(regURL);
try {
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String insert_data = URLEncoder.encode("Username","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+
"&"+URLEncoder.encode("Password","UTF-8")+ "=" +URLEncoder.encode(pass,"UTF-8");
bufferedWriter.write(insert_data);
bufferedWriter.flush();
bufferedWriter.close();
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"ISO-8859-1");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String result="";
String line="";
StringBuilder stringBuilder = new StringBuilder();
while((bufferedReader.readLine())!= null){
stringBuilder.append(line).append("\n");
}
result = stringBuilder.toString();
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
else if(type.equals("login")){
System.out.println("In type = login");
String name1 = strings[1];
String pass1 = strings[2];
try{
URL url = new URL(loginURL);
try {
System.out.println("In type = login try");
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String login_data = URLEncoder.encode("Username","UTF-8")+"="+URLEncoder.encode(name1,"UTF-8")+
"&"+URLEncoder.encode("Password","UTF-8")+ "=" +URLEncoder.encode(pass1,"UTF-8");
bufferedWriter.write(login_data);
bufferedWriter.flush();
bufferedWriter.close();
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"ISO-8859-1");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String result="";
String line="";
StringBuilder stringBuilder = new StringBuilder();
while((bufferedReader.readLine())!= null){
stringBuilder.append(line).append("\n");
}
result = stringBuilder.toString();
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
statusOk="true";
Toast.makeText(context, "Successful", Toast.LENGTH_LONG).show();
//super.onPostExecute(s);
}
}
我已经检查了数据库和我的应用程序之间的连接是否已建立,并且应用程序正在运行且没有任何错误。
您可以在上面的代码中看到我创建了一个公共变量statusOk
并使用"false"
对其进行了初始化。 此变量告诉登录 class用户已输入正确的登录凭据。
您可以看到我已经将statusOk
的值更改为"true"
onPostExexute方法。
现在的问题是我的新活动在成功登录后没有从登录 class 打开。 请给我一个解决方案,如何在使用正确的登录凭据登录后打开新活动。
Asynctask 用于在后台线程中执行任务(不是您调用execute()
的主线程)。
因此,在您的代码中, if
语句在您调用execute()
之后立即执行,它仍然将变量statusOK
存储为 false。 因此条件为假。
asynctask 的postExecute()
方法用于在主线程中执行代码,
我的建议:从Login
class 中删除if
条件,并检查postExecute()
中的if
条件,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.