我下面的代碼是爲我建立一個簡單的Android登錄應用程序,我想要它做的是直接用戶到某個頁面,如果登錄是正確的,目前用戶可以登錄並查看他們的個人資料,但是他們都被定向到相同的個人資料,無論他們是誰。我有兩個配置文件活動的NiallProfile.xml和AlannahProfile.xml,目前都被定向到NiallProfile,但我想改變它,以便如果輸入憑據'Alannah'和她的密碼,它將直接到她的頁面。有人可以告訴我如何做到這一點?如果可能的話,最好使用意圖,因爲我已經知道如何使用它們。下面的代碼檢查,看是否登錄成功,如果是,就會進入Nialls輪廓應用程序檢查誰登錄,並指示他們正確的頁面相應
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "";
if (type.equals("Login")) {
try {
String username = params[1];
String password = params[2];
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 post_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "utf-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "utf-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
@Override
protected void onPostExecute(String result) {
if (result.contains("Successful login")){
Intent profileIntent = new Intent(context, NiallProfile.class);
context.startActivity(profileIntent);
} else {
alertDialog.setMessage(result);
alertDialog.show();
}
}
您如何確定登錄是否成功?你從哪裏獲得用戶名和密碼?在你做這些事情的地方多發一些代碼,我會盡力幫助你。 –
該代碼從在線數據庫中獲取用戶名和密碼,我不認爲這是兩個用戶都可以登錄的問題,我將PHP代碼從這個問題中解脫出來,以免混淆人們,我只需要進行某種檢查這表示如果(用戶名='Alannah'和結果='登錄成功'){意圖profile2Intent =新的意圖(上下文,AlannahProfile.class – MonBoy175