2016-08-25 7 views
0

私はphp、json、mysql、およびhttpclientを使用するためのアンドロイドアプリケーションの初心者です。 私は登録とログインアプリケーションを作成しようとしています。登録に成功しました。しかし、ログインするときに問題があります。すべての入力電子メールとパスワードは常に「無効な電子メールまたはパスワード」というエラーを表示します。私も正しいメールとパスワードを入力しました。コードは以下の通りです。 login1.phpHttpclientを使用したAndroidログイン

<?php 
define('HOST','mxxxx.xxxxx.com'); 
define('USER','xxxxxx'); 
define('PASS','xxxxx'); 
define('DB','xxxxxx'); 

$con = mysqli_connect(HOST,USER,PASS,DB); 

$emailAddress = $_POST['email']; 
$passWord = $_POST['password']; 

$sql = "select * from user where email='$emailAddress' and password='$passWord'"; 

$res = mysqli_query($con,$sql); 

$check = mysqli_fetch_array($res); 

if(isset($check)){ 
echo 'success'; 
}else{ 
echo 'failure'; 
} 

mysqli_close($con); 
?> 

次いで(Eclipseの)LoginActivity.class

public class LoginActivity extends Activity{ 

    public static final String KEY_EMAIL="emailAddress"; 
    public static final String KEY_PASSWORD="passWord"; 

    private EditText etPassword, etEmail; 
    String passWord,emailAddress,email; 
    Button login; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 

     TextView registerScreen = (TextView) findViewById(R.id.link_to_register); 
     etEmail=(EditText)findViewById(R.id.ET_email); 
     etPassword=(EditText)findViewById(R.id.ET_password); 
     login=(Button)findViewById(R.id.btnLogin); 

     login.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       //fullName= etFullName.getText().toString(); 
       passWord=etPassword.getText().toString(); 
       emailAddress=etEmail.getText().toString(); 

       if (!isValidEmail(emailAddress)) { 
        etEmail.setError("Please Enter Valid Email"); 
        etEmail.requestFocus(); 
       } 
       else if (!isValidPassword(passWord)){ 
        etPassword.setError("Password min. 6 characters"); 
        etPassword.requestFocus(); 
       }else{ 

        //new LoginAsync().execute(emailAddress,passWord); 

        login(emailAddress,passWord); 
        // new BackgroundTaskLogin(LoginActivity.this).execute(emailAddress,passWord); 
         etEmail.setText(""); 
         etPassword.setText(""); 
       } 

      } 
    }); 
     // Listening to register new account link 
     registerScreen.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // Switching to Register screen 
       Intent i = new Intent(getApplicationContext(), RegisterActivity.class); 
       startActivity(i); 
      } 
     }); 
    } 

    private void login(final String emailAddress, final String passWord) { 
    class LoginAsync extends AsyncTask<String, Void, String>{ 
     //private Context context; 
      public static final String KEY_EMAIL = "email"; 
      private Dialog loadingDialog; 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loadingDialog = ProgressDialog.show(LoginActivity.this, "Please wait", "Loading..."); 
      } 

      @Override 
      protected String doInBackground(String... params) { 
       String emailAddress = params[0]; 
       String passWord = params[1]; 

       InputStream is = null; 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("email", emailAddress)); 
       nameValuePairs.add(new BasicNameValuePair("password", passWord)); 
       //return nameValuePairs; 

       String result = null; 

       try{ 
        HttpClient httpClient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost("http://xxxx.com/login1.php"); 
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        HttpResponse response = httpClient.execute(httpPost); 
        HttpEntity entity = response.getEntity(); 

        is = entity.getContent(); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return result; 
      } 

      @Override 
      protected void onPostExecute(String result){ 
       String s = result.trim(); 
       loadingDialog.dismiss(); 
       if(s.equalsIgnoreCase("success")){ 
        Intent intent = new Intent(LoginActivity.this, ActivityUserProfile.class); 
        intent.putExtra(KEY_EMAIL, emailAddress); 
        startActivity(intent); 
        finish(); 

       }else { 
        Toast.makeText(getApplicationContext(), "Invalid Email or Password", Toast.LENGTH_LONG).show(); 
       }} 
     } 

    new LoginAsync().execute(emailAddress, passWord); 

    } 


    private boolean isValidEmail(String email) { 
     String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" 
       + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 

     Pattern pattern = Pattern.compile(EMAIL_PATTERN); 
     Matcher matcher = pattern.matcher(email); 
     return matcher.matches(); 
    } 

    // validating password with retype password 
    private boolean isValidPassword(String pass) { 
     if (pass != null && pass.length() >= 6) { 
      return true; 
     } 
     return false; 
    } 

     } 
+0

** WARNING **:あなたが使用する必要がありますmysqli' '使用する場合は、[パラメータ化クエリ](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)と[ 'bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)を使用して、クエリにユーザーデータを追加します。 **重大な[SQLインジェクションのバグ](http://bobby-tables.com/)を作成したため、文字列の補間または連結を使用してこれを実行しないでください。 ** '$ _POST'や' $ _GET'データを直接クエリに入れないでください。誰かがあなたのミスを悪用しようとすると、非常に危険です。 – tadman

+0

ログインサービスコールの実行中にパラメータとして渡す値をログに記録しようとしましたか?そうでない場合は、正しい値を渡しているかどうかを確認してください。 – Mahamadali

+0

私を手伝ってくれて、どうすればいいですか? – Sasya

答えて

0
httpPost.setEntity(new StringEntity(body, HTTP.UTF_8)); 

又は

HttpPost httpPost = new HttpPost("http://someurl.com"); 
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8)); 

Refer 1

Refer 2

+0

まだ同じ出力@Karthik – Sasya

関連する問題