i have some trouble here with my server and app.. so i want to pass object/var from server with php..and i having trouble how to retrieve it.. in this case i want to pass id,username to my app..
this is my code user.java
public class User {
private String username="";
private String password="";
private int id;
}
on my login form, this is my code
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();
}
});
}
then on my php server side :
$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;
}
is it my code on php server correct? (just echo username and id). if my code correct then how to save username and id to my object(class)? if i just echo username,id then show it to textview and get the value and set it on my class, is it still object oriented ?my friend make me confuse about object oriented programming
any help will be appreciated..thanks..
It looks like you’re handling the response on the Java side so its really up to you how to persist it. You could use the preferences classes or use an object relational mapper (orm) for sqlite3. Examples for both are easy to come by