![](/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.