2017-03-05 11 views
-1

静的変数isSuccessfulという変数は、誰かが正常にログインできた場合はtrue、そうでない場合はfalseになります。私はデフォルトでfalseに設定しています。私が書いたPHPスクリプトは、 "loginsuccess"というメッセージを送り、それをonProgressUpdateパラメータに格納します。私はそれがパラメータに格納されているかどうかを確認するためにデバッグし、コンパイルはそれを示しています。私はなぜisSuccessfulがtrueに切り替えられていないのか理解できません。私はそれをするように設定しました。そのようなことが起こると、loginアクティビティで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

で文字列を比較していないが、ここで – nogad

+0

が追加@nogad何PHPありませんPHP – user2626734

+0

SQLクエリは、1)が開いています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はまだfalseのままです。 – user2626734

+0

ええ、それは私の愚かな間違いでした。あなたのソリューションは機能しました。ありがとう。 – user2626734

+0

ところで、tgatのようなメソッドをタイプすると、何が呼び出されますか?このメソッドを変数の中に入れます。それは何と呼ばれていますか? – user2626734

関連する問題