2017-03-05 84 views
-1

所以我有一個名爲isSuccessful的靜態變量,所有變量都應該這樣做,如果某人能夠成功登錄,則爲true,否則爲false。我已經設置爲默認爲false。我寫的php腳本發送消息「loginsuccess」並將其存儲在onProgressUpdate參數中。我調試,看看這是什麼被存儲在參數中,編譯說它是。那麼我無法弄清楚爲什麼isSuccessful沒有被切換爲真。我決定這樣做。一旦發生這種情況,我將登錄活動稱爲homeScreen活動。 LoginTask:雖然條件符合,但布爾值沒有評估爲真

public class LogInTask extends AsyncTask<String, String,String> { 
    public Scanner reader; 
    Formatter writer; 
    Context mcontext; 

    //if Login was successful 
    public static boolean isSuccessful; 

    LogInTask(Context context) 
    { 
     mcontext = context; 

    } 

    URL url; 
    URLConnection con; 
    String output = ""; 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     isSuccessful=false; 
     try { 
      url = new URL("http://192.168.1.75:1234/login.php"); 
      con = url.openConnection(); 
      //allows to send information 
      con.setDoOutput(true); 
      //allows to receive information 
      con.setDoInput(true); 
      writer = new Formatter(con.getOutputStream()); 
      //Sends login information to SQL table 
      writer.format("user_name="+params[0]+"&password="+params[1]); 
      writer.close(); 

      //Reads input 
      reader = new Scanner(con.getInputStream()); 
      while(reader.hasNext()) 
      { 

       output+= reader.next(); 
      } 
      reader.close(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     publishProgress(output); 

     return output; 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
     Toast.makeText(mcontext, values[0],Toast.LENGTH_LONG).show(); 
     if(values[0]=="loginsuccess") 
      isSuccessful = true; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
    } 
} 

LogInActivity:

public class LogInActivity extends Activity { 
    private Typeface fontRobo; 
    private TextView logoText; 
    private EditText userName; 
    private EditText passWord; 

    private TextView dontHave; 
    private TextView signUp; 

    private Button logIn; 
    Intent i; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_log_in); 
     i = new Intent(this, HomeActivity.class); 

     //Logo 
     logoText = (TextView)findViewById(R.id.Logo); 
     fontRobo = Typeface.createFromAsset(this.getAssets(),"fonts/ROBO.ttf"); 
     logoText.setText("ArtSpace"); 
     logoText.setTypeface(fontRobo); 

     //Don't have an account? 
     dontHave = (TextView) findViewById(R.id.Donthave); 
     dontHave.setTypeface(fontRobo); 

     //Sign Up 
     signUp = (TextView) findViewById(R.id.signUP); 
     signUp.setTypeface(fontRobo); 

     userName = (EditText) findViewById(R.id.userName); 
     passWord = (EditText) findViewById(R.id.passWord); 

     logIn = (Button) findViewById(R.id.LogIn); 

    } 
    //Log in button event 
    public void logInClick(View view) 
    { 
     final LogInTask task = new LogInTask(LogInActivity.this); 
     task.execute(userName.getText().toString(), passWord.getText().toString()); 
     if(LogInTask.isSuccessful) 
      startActivity(i); 

    } 

PHP:

<?php 
    require "conn.php"; 
    $user_name = $_POST['user_name']; 
    $user_pass = $_POST['password']; 
    $mysql_qry = "SELECT * FROM login WHERE UserName LIKE '$user_name' AND Password LIKE '$user_pass';"; 
    $result = mysqli_query($conn,$mysql_qry); 

    if(mysqli_num_rows($result) == true) 
    { 
     echo "login success"; 
    } 
    else 
    { 
     echo "login not success"; 

    } 
?> 
+3

比較字符串沒有PHP這裏 – nogad

+0

@nogad加入PHP – user2626734

+0

你的SQL查詢1)開放sql注入,2)使用LIKE代替精確比較,3)讓數據庫進行字符串比較,可能不區分大小寫,4)使用以明文形式存儲的密碼。 – sisve

回答

1

task.execute()是的AsyncTask又名它需要時間來執行。但是在你打電話之後,你正在檢查。您需要在onPostExecute()區塊中檢查isSuccessful。 事情是這樣的:

final LogInTask task = new LogInTask(LogInActivity.this){ 
    @Override 
    protected void onPostExecute(String s) { 
    if(LogInTask.isSuccessful) 
     startActivity(i); 
    }}; 
    task.execute(userName.getText().toString(), passWord.getText().toString()); 

PS:別的東西,不==使用.equals()

if(values[0].equals("loginsuccess")) 
+0

嘗試過,仍然沒有運氣。再次調試它,s等於「loginsuccess」,但仍然由於某種原因,或者其他isSuccessful仍然是錯誤的。 – user2626734

+0

是的,這是我的一個愚蠢的錯誤。你的解決方案奏效謝謝。 – user2626734

+0

順便說一句,當你輸入像tgat這樣的方法時,它叫什麼名字?你把這個方法放在變量裏面。那叫什麼? – user2626734