2011-07-03 25 views
0

この時点で、ユーザにユーザ名とパスワードを入力してもらいます(データは@MySQLを擬似します)。 しかし、動作しません。認証できません

マイfile.php:

<?php 
    include("ConnectDatabase.php"); 
    $Username = $_POST['Username']; 
    $Password = $_POST['Password']; 

    $q = mysql_query("SELECT Username, Password FROM Users 
        where Username = '".$Username."' and 
        Password = '".$Password."'"); 

    if(mysql_num_rows($q) > 0){ 
     print json_encode($q); 
    }else{ 
     print "1"; 
    } 
    mysql_close(); 
    ?> 

私のjavaファイル:

public class Authentication extends Activity implements OnClickListener, 
     OnKeyListener, OnCheckedChangeListener { 

    /** Called when the activity is first created. */ 

    ArrayList<NameValuePair> authentication; 
    String passIn, userIn, result; 
    EditText username, password; 
    CheckBox remember; 
    Button b_login; 
    InputMethodManager inputManager; 
    InputStream is; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // userIn = username.getText().toString(); 
     // passIn = password.getText().toString(); 
     username = (EditText) findViewById(R.id.usrname); 
     password = (EditText) findViewById(R.id.password); 
     inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     remember = (CheckBox) findViewById(R.id.remember); 
     remember.setOnCheckedChangeListener(this); 
     b_login = (Button) findViewById(R.id.login); 
     b_login.setOnClickListener(this); 
     username = (EditText) findViewById(R.id.usrname); 
     username.setOnKeyListener(this); 
     password = (EditText) findViewById(R.id.password); 
     password.setOnKeyListener(this); 

    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch (v.getId()) { 
     case R.id.login: 
      while (true) { 
       userIn = username.getText().toString(); 
       passIn = password.getText().toString(); 
       try { 
        sendAuthenticationData(userIn, passIn); 
        break; 
       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (WrongInputException e) { 
        Toast.makeText(Authentication.this, "Invalid username or password", Toast.LENGTH_LONG); 

       } 
//    break; 
      } 
      Intent intent = new Intent(this, ApplicationMenus.class); 
      this.startActivity(intent); 
      clearText(username,password); 

      break; 
     } 
    } 

    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     // TODO Auto-generated method stub 
     if ((event.getAction() == KeyEvent.ACTION_DOWN) 
       && (keyCode == KeyEvent.KEYCODE_ENTER)) { 
      // Perform action on key press 

      inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      return true; 
     } 
     return false; 
    } 


    public void clearText(EditText usr, EditText pass) { 
     usr.setText(""); 
     pass.setText(""); 
    } 

    public void sendAuthenticationData(String username, String password) 
      throws ClientProtocolException, IOException, WrongInputException { 

     authentication = new ArrayList<NameValuePair>(); 
     authentication.add(new BasicNameValuePair("Username", userIn)); 
     authentication.add(new BasicNameValuePair("Password", passIn)); 
     this.sendData(authentication); 
    } 

    public void sendData(ArrayList<NameValuePair> data) 
      throws ClientProtocolException, IOException, WrongInputException { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(
       "path/Authentication.php"); // I use real path here 
     httppost.setEntity(new UrlEncodedFormEntity(data)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
//  is = entity.getContent(); 
     String temp = EntityUtils.toString(entity); 

     if (temp.equals("1")) { 
      throw new WrongInputException(); 
     } 

    }} 

これは、PHPサーバーとの接続コード、およびJSONを書くのは初めてです。私はJSONとかなり混乱していますが、私は多くのサンプルに従おうとしました。

$Username = $_POST['Username']; 

そして、あなたの第二の問題は、おそらくこれです:あなたが忘れてしまった最初のことは、着信の変数のmysql_real_escape_stringある任意のヘルプ

+0

質問は何ですか?また、何かエラーがありましたか?あなたのアプリはクラッシュしますか?もしそうなら、logcatトレースを貼り付けることができますか? – Cristian

+0

私はそれがなぜ機能しないのか分かりません。私は何の誤りもなかった。 – Yoo

+0

あなたのコードは安全でないため、PDOの準備文を調べてください。> http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ – Alfred

答えて

2

のために感謝し

if (mysql_num_rows($q) > 0) { 
     print json_encode($q); 

$q変数はmysql_結果であるハンドル。 JSON表現はできません。

 print json_encode(mysql_fetch_assoc($q)); 

これで、ユーザー名/パスワードを含む結果配列/オブジェクトが少なくとも取得されます。たぶん、別の簡単な数値結果を返信すると、print "2";などが返ってくるほうが簡単でしょう。

また、json_encodeが確実に利用できるようにPHP 5.2以降を実行したことを確認しましたか?

関連する問題