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 然后将其显示给 textview 并获取值并将其设置在我的类上,它仍然是面向对象的吗?我的朋友让我对面向对象编程感到困惑

任何帮助将不胜感激..谢谢..

4

2 回答 2

0

看起来您正在处理 Java 端的响应,因此如何持久化它真的取决于您。您可以使用首选项类或对 sqlite3 使用对象关系映射器(orm)。两者的例子都很容易找到

于 2012-10-07T08:03:03.337 回答
0

我强烈建议你使用 JSON,你在 php 和 Android sdk 中都有它,它会让你的生活轻松很多。不过,在我看来,您如何“解析”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 回答