i making android application allow login register , data like, user id, phone number,... processes works well, data comes null.
here function in android login only, same registration , getting data other params
private class asyncdataclass extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { httpparams httpparameters = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparameters, 5000); httpconnectionparams.setsotimeout(httpparameters, 5000); httpclient httpclient = new defaulthttpclient(httpparameters); httppost httppost = new httppost(params[0]); string jsonresult = ""; //assigning params edittext(s) try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("username", params[1])); namevaluepairs.add(new basicnamevaluepair("password", params[2])); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); jsonresult = inputstreamtostring(response.getentity().getcontent()).tostring(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return jsonresult; } @override protected void onpreexecute() { super.onpreexecute(); } @override protected void onpostexecute(string result) { super.onpostexecute(result); system.out.println("resulted value: " + result); if(result.equals("") || result == null){ pdialog.dismiss(); toast.maketext(mainactivity.this, "server connection failed", toast.length_long).show(); return; } int jsonresult = returnparsedjsonobject(result); if(jsonresult == 0){ pdialog.dismiss(); toast.maketext(mainactivity.this, "invalid username or password", toast.length_long).show(); return; } //when login correct //get_phone , get_id null (first problem) if(jsonresult == 1){ sharedpreferences preferences = getsharedpreferences("myprefernces", mode_private); sharedpreferences.editor editor = preferences.edit(); editor.putboolean("loggin", checkres); editor.putstring("saved_username", enteredusername); editor.putstring("saved_phone", get_phone); editor.putstring("saved_id",get_id ); editor.apply(); intent intent = new intent(mainactivity.this, loginactivity.class); intent.putextra("username", enteredusername); intent.putextra("message", "you have been login direct"); startactivity(intent); finish(); } } private stringbuilder inputstreamtostring(inputstream is) { string rline = ""; stringbuilder answer = new stringbuilder(); bufferedreader br = new bufferedreader(new inputstreamreader(is)); try { while ((rline = br.readline()) != null) { answer.append(rline); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return answer; } } //getting json result private int returnparsedjsonobject(string result){ jsonobject resultobject = null; int returnedresult = 0; try { resultobject = new jsonobject(result); returnedresult = resultobject.getint("success"); get_phone = resultobject.getstring("phonenum"); get_id = resultobject.getstring("id"); } catch (jsonexception e) { e.printstacktrace(); } return returnedresult; }
here php file android app hits..
<?php require_once 'include/user.php'; $username = ""; $password = ""; $email = ""; if(isset($_post['username'])){ $username = $_post['username']; } if(isset($_post['password'])){ $password = $_post['password']; } if(isset($_post['email'])){ $email = $_post['email']; } // instance of user class $userobject = new user(); //functions.. // registration of new user if(!empty($username) && !empty($password) && !empty($email)){ $hashed_password = md5($password); $json_registration = $userobject->createnewregisteruser($username, $hashed_password, $email); echo json_encode($json_registration); } // user login if(!empty($username) && !empty($password) && empty($email)){ $hashed_password = md5($password); $json_array = $userobject->loginusers($username, $hashed_password); echo json_encode($json_array); } // getting data >> here sent id database via $username 'int' if(!empty($username) && empty($password) && empty($email)){ $json_array = $userobject->getdata($username); echo json_encode($json_array); } ?>
here class called..
private $db; private $db_table = "users"; private $result1 = ""; private $result2 = ""; public function __construct(){ $this->db = new dbconnect(); } public function isloginexist($username, $password){ $query1 = "select `id` users username = '$username' , password = '$password' limit 1"; $result1 = mysqli_query($this->db->getdb(), $query1); if(mysqli_num_rows($result1) > 0){ $query2 = "select `email` users username = '$username' limit 1"; $result2 = mysqli_query($this->db->getdb(),$query2); mysqli_close($this->db->getdb()); return true; } mysqli_close($this->db->getdb()); return false; } public function isidexist($username){ $query1 = "select `username` users 'id' = '$username' limit 1"; $result1 = mysqli_query($this->db->getdb(),$query1); if(mysqli_num_rows($result1) > 0){ $query2 = "select `email` `users` `id` = '$username' limit 1"; $result2 = mysqli_query($this->db->getdb(),$query2); mysqli_close($this->db->getdb()); return true; } mysqli_close($this->db->getdb()); return false; } public function createnewregisteruser($username, $password, $email){ $json = array(); $query = "insert users (username, password, email) values ('$username', '$password', '$email')"; $inserted = mysqli_query($this->db->getdb(), $query); if($inserted == 1){ $query1 = "select `id` `users` `username` = '$username' limit 1"; $result1 = mysqli_query($this->db->getdb(), $query1); $json['success'] = 1; $json['id'] = $result1; }else{ $json['success'] = 0; $json['id'] = ""; } mysqli_close($this->db->getdb()); return $json; } public function getdata($username){ $json = array(); $isitexists = $this->isidexist($username); if($isitexists){ $json['success'] = 1; $json['username'] = $result1; $json['phonenum'] = $result2; }else{ $json['success'] = 0; $json['username'] = ""; $json['phonenum'] = ""; } return $json; } public function loginusers($username, $password){ $json = array(); $canuserlogin = $this->isloginexist($username, $password); if($canuserlogin){ $json['success'] = 1; $json['id'] = $result1; $json['phonenum'] = $result2; }else{ $json['success'] = 0; $json['id'] = ""; $json['phonenum'] = ""; } return $json; } } ?>
try code that.. if(isset($inserted))
public function createnewregisteruser($username, $password, $email){ $query = "insert users (username, password, email) values ('$username', '$password', '$email')"; $inserted = mysqli_query($this->db->getdb(), $query); if(isset($inserted) ){ $json['success'] = 1; }else{ $json['success'] = 0; } mysqli_close($this->db->getdb()); return $json; }
Comments
Post a Comment