我的服务器和应用程序在这里遇到了一些问题。所以我想使用 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 并获取值并将其设置在我的类上,它仍然是面向对象的吗?我的朋友让我对面向对象编程感到困惑
任何帮助将不胜感激..谢谢..