ここでサーバーとアプリに問題があります..だから私は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をエコーしてテキストビューに表示し、値を取得してクラスに設定すると、それはまだオブジェクト指向ですか?私の友人はオブジェクト指向プログラミングについて混乱させます
どんな助けでも大歓迎です..ありがとう..