2015-12-23 67 views
0

服務器端的所有東西都按其應有的方式工作。我的問題在於,如果HTTP響應是「註冊成功!」,我不確定如何返回到登錄頁面。如果在Android中註冊成功,我該如何進入登錄活動?

我的Login和Register類都依賴於BackgroundTask來處理異步操作。

這裏是BackgroundTask的代碼。

public class BackgroundTask extends AsyncTask<String, Void, String> { 
    AlertDialog mAlertDialog; 
    Context context; 
    private CheckTask mCheckTask; 


    BackgroundTask(Context context, Boolean login, Boolean register) { 
     this.context = context; 
     mCheckTask = new CheckTask(login, register); 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     String reg_url = "http://www.myegotest.com/register.php"; 
     String login_url = "http://www.myegotest.com/login.php"; 

     ////////////////////////REGISTER SCRIPT///////////////////////////// 
     if (mCheckTask.getIsRegisterTask()) { 
      String first_name = params[0]; 
      String last_name = params[1]; 
      String username = params[2]; 
      String password = params[3]; 
      try { 
       //Set the URL we are working with 
       URL url = new URL(reg_url); 

       //Open a url connection and set params for the url connection 
       HttpURLConnection LucasHttpURLConnection = (HttpURLConnection) url.openConnection(); 
       LucasHttpURLConnection.setRequestMethod("POST"); 
       LucasHttpURLConnection.setDoOutput(true); 
       LucasHttpURLConnection.setDoInput(true); 
       //Retrieve the output stream 
       OutputStream os = LucasHttpURLConnection.getOutputStream(); 
       //Create a buffered writer to write the data to the output stream output stream. 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
       //Encode Data we are sending on the output stream 
       String data = URLEncoder.encode("first_name", "UTF-8") + "=" + URLEncoder.encode(first_name, "UTF-8") + "&" + 
             URLEncoder.encode("last_name", "UTF-8") + "=" + URLEncoder.encode(last_name, "UTF-8") + "&" + 
             URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" + 
             URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); 
       //Write the data to the output stream, and close buffered writer 
       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       //Close output stream 
       os.close(); 
       //InputStream to get response 
       InputStream IS = LucasHttpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1")); 
       String response = ""; 
       String line; 
       while ((line = bufferedReader.readLine()) != null) { 
        response += line; 
       } 
       bufferedReader.close(); 
       IS.close(); 
       //LucasHttpURLConnection.disconnect(); 
       return response; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      //////////////////////////////////LOGIN SCRIPT///////////////////////////////////////// 
     } else if (mCheckTask.getIsLoginTask()) { 
      String username = params[0]; 
      String password = params[1]; 
      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("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" + 
             URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); 
       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       outputStream.close(); 
       //InputStream to get response 
       InputStream IS = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1")); 
       String response = ""; 
       String line; 
       while ((line = bufferedReader.readLine()) != null) { 
        response += line; 
       } 
       bufferedReader.close(); 
       IS.close(); 
       httpURLConnection.disconnect(); 
       return response; 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPreExecute() { 
     if (mCheckTask.getIsLoginTask()) { 
      mAlertDialog = new AlertDialog.Builder(context).create(); 
      mAlertDialog.setTitle("Login Information... "); 
     } else { 
      mAlertDialog = new AlertDialog.Builder(context).create(); 
      mAlertDialog.setTitle("Register Information... "); 
     } 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     if (result.equals("Please choose another username")) { 
      mAlertDialog.setMessage(result); 
      mAlertDialog.setButton("Ok", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
       } 
      }); 
      mAlertDialog.show(); 

     } else if (result.equals("Registration success!")) { 
      mAlertDialog.setMessage(result); 
      mAlertDialog.setButton("Ok", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 

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

回答

0

如果從構造函數參數中持有的上下文引用是一個活動,那麼可以考慮將構造函數簽名和字段類型更改爲Activity。從那裏你可以打電話mActivity.finish();對話框點擊監聽器。這將導致活動關閉,並退回堆棧後面的一個。

您也可以使用進行調查,以Activity.setResult(int, Intent)信息傳送回使用onActivityResult(int, int, Intent)

以前的活動這是很方便的預填充登錄表單時註冊活動關閉。

http://developer.android.com/training/basics/intents/result.html

0

您可以創建在您檢查您的數據庫currentuser調度方法。我正在使用Parse,但是您可以使用preferenceManager與此處示例中的相同Preference manager example here

Public class Dispatch extends ActionBarActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    // Check if there is current user info 
    if (ParseUser.getCurrentUser() != null) { 
     // Start an intent for the logged in activity 
     startActivity(new Intent(this, Home.class)); 
    } else { 
     // Start and intent for the logged out activity 
     startActivity(new Intent(this, login.class)); 
    } 
} 

} 
相關問題