2016-11-15 4 views
1

ログインにjsonを使用しようとしました。私のPHPはこの結果を返します:android studioのログインバグを修正する方法

[{"name":"cynthia","password":"123456"},{"name":"John","password":"123456"}] 

これらはmySQLのデータです。

この結果をアンドロイドスタジオで比較したいのですが、うまく動作していないことがわかりました。常にelseステートメントに移動します。

public class LoginActivity extends AppCompatActivity { 

    public EditText username; 
    public EditText password; 
    private ArrayList<String> usernameList = new ArrayList<String>(); 
    private ArrayList<String> passwordList = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     username = (EditText) findViewById(R.id.username); 
     password = (EditText) findViewById(R.id.password); 
    } 

    public void Login2(View view) { 
     if (username.getText().length() == 0 || password.getText().length() == 0) { 
      openDialog(view); 
     } else { 
      new AsyncRetrieve().execute(); 
      if(usernameList.contains(username)&&passwordList.contains(password)){ 
       successLogin(); 
      }else{ 
       openDialog(view); 
      } 

     } 
    } 

    public void openDialog(View view) { 
     new AlertDialog.Builder(this) 
       .setTitle(R.string.login_dialog_title) 
       .setMessage(R.string.login_dialog_msg) 
       .show(); 
    } 

    public void successLogin() { 
     Intent intent = new Intent(); 
     intent.setClass(this, HomePageActivity.class); 
     startActivity(intent); 
    } 

    private class AsyncRetrieve extends AsyncTask<Void, Void, String> { 
     @Override 
     protected String doInBackground(Void... params) { 
      return getVideoList(); 
     } 

     private String getVideoList() { 
      try { 
       URL url = new URL("http://localhost/soften/login_check1.php"); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       InputStream in = new BufferedInputStream(conn.getInputStream()); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
       String response = reader.readLine(); 

       //set the response from server as json array 
       JSONArray userArray = new JSONArray(response); 

       usernameList.clear(); 
       passwordList.clear(); 
       Log.d("username", "value = " + userArray.length()); 
       for (int i = 0; i < userArray.length(); i++) { 
        Log.d("i", "i = " + i); 
        //get json object from the json array 
        JSONObject json = userArray.getJSONObject(i); 
        Log.d("json", "" + json.length()); 
        //add object to the list for grid view 
        usernameList.add(json.getString("name")); 
        passwordList.add(json.getString("password")); 
       } 



       return null; 
      } catch (Exception ex) { 
       return ex.toString(); 
      } 
     } 
    } 
} 
+0

エンドポイントを保護しない限り、非常に安全でない方法のようです。ログインのためにデータベースから電話にすべてのユーザーの詳細をダウンロードすることは過度のようです。 –

答えて

1

あなたは)(long2あなたはusername.getText()とpassword.getTextに等しくなければならない

if(usernameList.contains(username)&&passwordList.contains(password)) 

あり、ERRO mと().MY英語は非常に良いではありませんが、あなたがする必要があります問題は、このラインである

+0

ご返信ありがとうございます。 .getText()を追加しましたが、まだ動作していません。ステートメントを に変更したときに、arraylistに必要な文字列が含まれていないようです(if!usernameList.contains(username.getText()。toString())){ 次のページに入ることができます。なぜなのかご存知ですか?どうもありがとう!! – cynthiangwm

0

を理解することができ

new AsyncRetrieve().execute(); 

は、それがバックを開始するとき、あなたのAsyncTaskを実行しますサーバーからの結果を待つことなくすぐにさらに進み、ユーザー名とパスワードを比較すると、これらのリストは空です。

private ArrayList<String> usernameList = new ArrayList<String>(); 
private ArrayList<String> passwordList = new ArrayList<String>(); 

ソリューション:

最初のソリューションは、結果は、サーバから受信するまで、それはあなたのAsyncTaskを停止します

new AsyncRetrieve().execute().get(); 

のようなあなたのAsyncTaskのコールへのget()メソッドを追加することですあなたの比較をしました。

もう1つの解決方法は、AsyncTaskでonPostExecute()をオーバーライドしてそこで比較を実行することです。

private class AsyncRetrieve extends AsyncTask<Void, Void, String> { 
     @Override 
     protected String doInBackground(Void... params) { 
      return getVideoList(); 
     } 

     private String getVideoList() { 
      try { 
       URL url = new URL("http://localhost/soften/login_check1.php"); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       InputStream in = new BufferedInputStream(conn.getInputStream()); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
       String response = reader.readLine(); 

       //set the response from server as json array 
       JSONArray userArray = new JSONArray(response); 

       usernameList.clear(); 
       passwordList.clear(); 
       Log.d("username", "value = " + userArray.length()); 
       for (int i = 0; i < userArray.length(); i++) { 
        Log.d("i", "i = " + i); 
        //get json object from the json array 
        JSONObject json = userArray.getJSONObject(i); 
        Log.d("json", "" + json.length()); 
        //add object to the list for grid view 
        usernameList.add(json.getString("name")); 
        passwordList.add(json.getString("password")); 
       } 



       return null; 
      } catch (Exception ex) { 
       return ex.toString(); 
      } 
     } 


     @Override 
     protected String onPostExecute(String result) { 
      if(usernameList.contains(username.getText())&&passwordList.contains(password.getText())){ 
       successLogin(); 
      }else{ 
       openDialog(view); 
      } 
     } 
    } 

これらの行をLogin2メソッドから削除します.getText()メソッドを追加することを忘れないでください。

このヘルプが必要です。

関連する問題