I am making Android Application that allow login register and get data like, user ID, phone number,... The Processes are works well, but the data that comes are always Null.
Here is the Function in Android For Login only, it the same for Registration and getting data but with 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 the Params from 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 the login is Correct
//get_phone and get_id are always 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 successfully login to 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 is the PHP file that The 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 a 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 I sent the ID to the database via $username but it is 'INT'
if(!empty($username) && empty($password) && empty($email)){
$json_array = $userObject->getdata($username);
echo json_encode($json_array);
}
?>
Here is the class which is 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` FROM users WHERE username = '$username' AND password = '$password' Limit 1";
$result1 = mysqli_query($this->db->getDb(), $query1);
if(mysqli_num_rows($result1) > 0){
$query2 = "SELECT `email` FROM users WHERE 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` FROM users WHERE 'id' = '$username' Limit 1";
$result1 = mysqli_query($this->db->getDb(),$query1);
if(mysqli_num_rows($result1) > 0){
$query2 = "SELECT `email` FROM `users` WHERE `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 into users (username, password, email) values ('$username', '$password', '$email')";
$inserted = mysqli_query($this->db->getDb(), $query);
if($inserted == 1){
$query1 = "SELECT `id` FROM `users` WHERE `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;
}
}
?>