2017-04-05 23 views
0

我下面的代碼是爲我建立一個簡單的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(); 
    } 
} 
+0

您如何確定登錄是否成功?你從哪裏獲得用戶名和密碼?在你做這些事情的地方多發一些代碼,我會盡力幫助你。 –

+0

該代碼從在線數據庫中獲取用戶名和密碼,我不認爲這是兩個用戶都可以登錄的問題,我將PHP代碼從這個問題中解脫出來,以免混淆人們,我只需要進行某種檢查這表示如果(用戶名='Alannah'和結果='登錄成功'){意圖profile2Intent =新的意圖(上下文,AlannahProfile.class – MonBoy175

回答

0

在部分地方你檢查if (username == "Alannah")店這個變量的地方在那裏,你可以在你的onPostExecute訪問它,或甚至更好的讓你的onPostExecute接受額外的參數,如onPostExecute(String username, String results)

然後只是做一個檢查:

if (result.contains("Successful login")){ 
    if(username == "Alannah") { 
     Intent profileIntent = new Intent(context, AlannahProfile.class); 
     context.startActivity(profileIntent); 
    } else { 
     Intent profileIntent = new Intent(context, NiallProfile.class); 
     context.startActivity(profileIntent); 
    } 

} else { 
     alertDialog.setMessage(result); 
     alertDialog.show(); 
} 

如果以後你會支持更多的用戶,可以考慮使用switch聲明而不是if-else

+0

好吧,謝謝,我已經添加了一些額外的代碼,告訴你如何登錄的作品,你 – MonBoy175

+0

嘗試在doInBackground方法之外聲明String用戶名,然後只用username = params [1];在postExecute中對其進行賦值,然後按照我的說法進行操作,如果它允許您在doInBackground之外賦值。你可能需要聲明靜態到你的用戶名。 –

+0

它說用戶名永遠不會被使用 – MonBoy175

0

你可以試試下面的代碼:

String username = null; 

你的方法之外,使這一variable是在類的所有方法訪問。

然後在您的doInBackground()方法中,將其分配給全局用戶名變量。

然後在onPostExecute()檢查登錄是否成功,然後檢查哪個用戶已經登錄並相應地重定向。

相關問題