2017-01-11 43 views
2

我想包括一個代碼,當用戶註冊已在我的應用中使用的用戶名時,他/她會得到一個敬酒「用戶名已被佔用」。如何通過「用戶名已被佔用」來防止重複登錄活動

Register.java

public class Register extends AppCompatActivity { 
EditText regEmail, regPassword; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 
    regEmail = (EditText)findViewById(R.id.reg_email); 
    regPassword = (EditText)findViewById(R.id.reg_password); 
} 
public void OnReg(View view) { 
    String strEmail = regEmail.getText().toString(); 
    String strPassword = regPassword.getText().toString(); 
    String type = "register"; 
    BackgroundWorker backgroundWorker = new BackgroundWorker(this); 
    backgroundWorker.execute(type, strEmail, strPassword); 
}} 

BackgroundWorker.java

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 = "http://10.93.22.231/login.php"; 
     String register_url = "http://10.93.22.231/register.php"; 
     if (type.equals("login")){ 
      try { 
       String email = 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("email","UTF-8")+"="+URLEncoder.encode(email,"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(); 
      } 
     } else if (type.equals("register")){ 
      try { 
       String regEmail = params[1]; 
       String regPassword = params[2]; 
       URL url = new URL(register_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("email","UTF-8")+"="+URLEncoder.encode(regEmail,"UTF-8")+"&" 
         +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(regPassword,"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(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPreExecute() { 
     alertDialog = new AlertDialog.Builder(context).create(); 
     alertDialog.setTitle("Login Status"); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     alertDialog.setMessage(result); 
     if (result.contains("success")) { 
      Intent intent = new Intent(context, MainActivity.class); 
      context.startActivity(intent); 
     } else { 
      alertDialog.show(); 
     } 
    } 

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

Register.php

<?php 
require "conn.php"; 

$email = $_POST["email"]; 
$password = $_POST["password"]; 

$mysql_qry = "INSERT INTO users (email, password) VALUES ('$email','$password')"; 

if($conn->query($mysql_qry) === TRUE){ 
echo "Insert successful"; 
} else { 
echo "Insert failed, please try again."; 
} 
$conn->close(); 
?> 
+0

()方法中,你有串。你可以將它轉換爲JSONObject,它應該包含你的實際消息。然後獲取該信息並烘烤。 – Noorul

+0

'onPostExecute'你在結果中得到了什麼..? –

+0

@jun Guo您的php代碼不包含任何用戶驗證查詢或code.add驗證查詢或代碼並嘗試 – Noorul

回答

1
Toast.makeText(YourActivtyOrContext,"Username Taken", Toast.LENGTH_LONG).show(); 
1

嘿,請檢查此代碼,也許它可以解決您的問題。這是簡單的代碼。

if (ConnectivityDetector.isConnectingToInternet(RegisterActivity.this)) { 
      JSONObject jsonObjectInput = new JSONObject(); 

      jsonObjectInput.put(WebField.REGISTER_USER.REQUEST_USER_NAME, 
        edtUserName.getText().toString()); 

      String mode = "RegisterUser"; 

      new GetJsonWithCallBack(RegisterActivity.this, jsonObjectInput, 
        1, mode, new OnUpdateListener() { 
       @Override 
       public void onUpdateComplete(JSONObject jsonObject, 
              boolean isSuccess) { 

        if (isSuccess) { 

         try { 

          if (jsonObject != null) { 
           if (jsonObject.has("userDetail")) { 
            JSONObject jsonUserDetails = jsonObject.getJSONObject("userDetail"); 

            RegisterData regData = new RegisterData(); 

            regData.setUserName(jsonUserDetails.getString(WebField.REGISTER_USER.RESPONSE_USER_NAME)); 

            SessionManager.saveData(RegisterActivity.this, regData); 

            finish(); 
           } 
          } 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } else { 

         if (jsonObject != null) { 
          try { 
           String Status = jsonObject.getString("status"); 

           String message = jsonObject.getString("message"); 

           if (message.equalsIgnoreCase("User already Exists")) { 
            GlobalMethod.showAlert(RegisterActivity.this, "User name already exists..!!"); 

           } else if (message.equalsIgnoreCase("Email id already Exists")) { 
            GlobalMethod.showAlert(RegisterActivity.this, "Email id already exists..!!"); 

           } else if (message.equalsIgnoreCase("Mobile no already Exists")) { 
            GlobalMethod.showAlert(RegisterActivity.this, "Mobile no already exists..!!"); 

           } else { 
            GlobalMethod.showAlert(RegisterActivity.this, jsonObject.getString("message")); 
           } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } else { 
         } 
        } 
       } 
      }).execute(); 
     } else { 
      AlertDialogUtility.showInternetAlert(RegisterActivity.this); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
1

上後執行,你可以像這樣

一些事情也是在服務器級別,如果用戶名已經存在,你不應該保存記錄,而不是說你應該通過該用戶名已經存在

@Override 
    protected void onPostExecute(String result) { 

     if (result.contains("success")) { 
      Intent intent = new Intent(context, MainActivity.class); 
      context.startActivity(intent); 
     } else { 
      Toast.makeText(context,result, Toast.LENGTH_LONG).show(); 
     } 
    } 
1

嘗試這種方式

@Override 
    protected void onPostExecute(String result) { 
    alertDialog.setMessage(result); 
    if (result.contains("success")) { 
     JSONObject json= new JSONObject(result); 
     if(json.has("message"){ 
     String message=json.getString("message"); 
     Toast.makeText(ctx,message,Toast.LENGTH_SHORT).show(); 
     } 
     Intent intent = new Intent(context, MainActivity.class); 
     context.startActivity(intent); 
    } else { 
     alertDialog.show(); 
    } 
} 

UPDATE

用戶驗證查詢

$mysql_qry="SELECT * FROM users WHERE email='$email'"; 

if($conn->query($mysql_qry) === TRUE){ 
}else{} 
在onPostExecute
+0

嘗試過,結果是一樣的,即使我使用相同的電子郵件地址註冊,應用也會將我帶入MainActivity。 –