0

ここでサーバーとアプリに問題があります..だから私はPHPでサーバーからオブジェクト/変数を渡したい..そしてそれを取得する方法に問題があります..この場合、ID、ユーザー名をアプリに渡したい. .

これは私のコードuser.java です

public class User   {
   private String username="";  
   private String password="";  
   private int id;
}

私のログインフォームでは、これは私のコードです

void login(){
    try{            

        httpclient=new DefaultHttpClient();

        httppost= new HttpPost("http://nervousme.vacau.com/check_login.php"); .

        nameValuePairs = new ArrayList<NameValuePair>(2);
        side variable name and php side variable name should be similar, 
        nameValuePairs.add(new BasicNameValuePair("username",et_username.getText().toString().trim()));  
        nameValuePairs.add(new BasicNameValuePair("password",et_password.getText().toString().trim())); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        response=httpclient.execute(httppost);


        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response : " + response); 
        runOnUiThread(new Runnable() {
            public void run() {
                tv.setText("Response from PHP : " + response);
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("Login Berhasil")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(LoginForm.this,"Login Berhasil", Toast.LENGTH_SHORT).show();
                }

            });

            Intent intent = new Intent(LoginForm.this,UserHome.class);
            intent.putExtra("username",et_username.getText().toString());  

            startActivity(intent);
        }else{
            showAlert();                
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}

public void showAlert(){
    LoginForm.this.runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(LoginForm.this);
            builder.setTitle("Login Gagal");
            builder.setMessage("Terdapat Kesalahan Pada Username/Password")  
                   .setCancelable(false)
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                       }
                   });                     
            AlertDialog alert = builder.create();
            alert.show();               
        }
    });
}

次に、私のphpサーバー側で:

$query_search = "select * from USER where username = '".$username."'
AND password = '".$password. "'";

$query_exec = mysql_query($query_search) or die(mysql_error());

$rows = mysql_num_rows($query_exec); //echo $rows;

if($rows == 0) { 
  echo "Login Gagal";   
}  else  {
  echo $username,$id;    
}

PHPサーバー上の私のコードは正しいですか?(ユーザー名とIDをエコーするだけです)。私のコードが正しい場合、ユーザー名とIDをオブジェクト(クラス)に保存する方法は? ユーザー名、IDをエコーし​​てテキストビューに表示し、値を取得してクラスに設定すると、それはまだオブジェクト指向ですか?私の友人はオブジェクト指向プログラミングについて混乱させます

どんな助けでも大歓迎です..ありがとう..

4

2 に答える 2

0

JSONを使用することを強くお勧めします。PHPとAndroidSDKの両方で使用でき、生活が大幅に楽になります。それにもかかわらず、http応答をどのように「解析」しているのかは少し奇妙に思えます。

if(response.equalsIgnoreCase("Login Berhasil")) { ... }

私はあなたがそれを返すサーバーを見ていません...

Androidのサンプルを見ることができます。たとえば、JSONの使用方法に関するいくつかの例があります。たとえば、/ android-sdk-macosx / samples / android-16 / SampleSyncAdapter / src / com / example / android / samplesync / client /NetworkUtilities.java

于 2012-10-07T09:02:15.927 に答える
0

Java側で応答を処理しているように見えるので、それを永続化する方法はあなた次第です。設定クラスを使用するか、sqlite3 のオブジェクト リレーショナル マッパー (orm) を使用できます。両方の例は簡単に手に入る

于 2012-10-07T08:03:03.337 に答える