user6588225 user6588225 - 4 months ago 27
PHP Question

display result of json data in textview of android

I am having one problem while displaying data in android app.This is my php.I have edited now to shorten the code.


if($imei_no!="" && $app_auid!="")
{
//chk query
$chk_query=$db1->query("select * from user where imei_number='$imei_no'");
$chk_cnt=$chk_query->rowCount();
if($chk_cnt>0)
{
$search_qry=$db1->query("select * from candidate_reception cr,candidate_counseling cc ,candidate_admission ca where cr.candidate_id=cc.candidate_id and cr.candidate_id=ca.candidate_id and cr.auid=ca.auid and cr.counseling_id=cc.counseling_id and cr.auid='$app_auid'");
$ser_count=$search_qry->rowCount();
if($ser_count>0)
{
$stud_row=$search_qry->fetch(PDO::FETCH_OBJ);

$candidate_id=$stud_row->candidate_id;
$auid = $stud_row->auid;
$usn_no = $stud_row->usn_no;
$sx = $stud_row->candidate_sex


//image path
if($inst_id==1){ $img_path = "1"; }
if($inst_id==2){ $img_path = "2"; }

$result=array();
array_push($result, array('name' => $candidate_name,'img_path'=>$img_path));

echo json_encode(array("can_data"=>$result));
echo "Success"
}
else
{
echo "Auid not Found.";
}
}
else
{
echo "Invalid IMEI Number.!!!";
}
}
?>


and this is my java code:

private static final String LOGIN_URL = "http://xxx.xxx.x.x/mobile_app/data_push.php";
private EditText editTextUserName;
private Button buttonLogin;

TelephonyManager tel;
TextView imei;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imei__val);

editTextUserName = (EditText) findViewById(R.id.editTextAUID);
buttonLogin = (Button) findViewById(R.id.btn);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login();
}
});

tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imei = (TextView) findViewById(R.id.textView2);
imei.setText(tel.getDeviceId().toString());

}

private void login(){
String username = editTextUserName.getText().toString().trim();
String imei_no = imei.getText().toString().trim();
userLogin(username,imei_no);
}

private void userLogin(final String username,final String imei_no){
class UserLoginClass extends AsyncTask<String,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(IMEI_Val.this,"Please Wait",null,true,true);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s=="Success"){

Intent intent = new Intent(IMEI_Val.this, CapturePhoto.class);
// intent.putExtra(USER_NAME,username);
startActivity(intent);
}

else{
Toast.makeText(IMEI_Val.this, s, Toast.LENGTH_LONG).show();
Log.d("Tag Name", "Log Message");
}
}

@Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("auid",params[0]);
data.put("imei",params[1]);
RegisterUserClass ruc = new RegisterUserClass();
String result = ruc.sendPostRequest(LOGIN_URL,data);
return result;
}
}
UserLoginClass ulc = new UserLoginClass();
ulc.execute(username,imei_no);
}

}


My problem is when am clicking on Login button, am getting json result in toast with student details after valid user inputs.I want to goto next activity if the the user inputs are valid and display other details like name,college etc.How can I do that?
Here am getting json result as response when I enter valid inputs.I could not go to next activity and display other details in text box even though am entering correct regno.

Answer

@user6588225

so your sending Json with Success message

so rather than using == try to use contains

so inside your onPostExecute(String s)

@Override
protected void onPostExecute(String s) {
     super.onPostExecute(s);
     loading.dismiss();
     if(s.contains("Success")){
         Intent intent = new Intent(IMEI_Val.this, CapturePhoto.class);
         // intent.putExtra(USER_NAME,username);
         startActivity(intent);
     }
     else{
       Toast.makeText(IMEI_Val.this, s, Toast.LENGTH_LONG).show();
       Log.d("Tag Name", "Log Message");
     }
}