Batz Batz - 4 months ago 18
Android Question

My android cant connect to my webserver

Here is my code to my android with the connection to my database using the

task1
. Can anybody help me here figure out my problem. Thanks everyone. It also works fine on localhost. :)

Android



package com.example.kun.carkila;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
final String LOG = "MainActivity";
Button btnLogin;
EditText etUsername,etPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

etUsername = (EditText)findViewById(R.id.etUsername);
etPassword = (EditText)findViewById(R.id.etPassword);
btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);

}
@Override
public void onClick(View v) {
HashMap postData = new HashMap();
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
postData.put("username",username);
postData.put("password", password);

PostResponseAsyncTask task1 = new PostResponseAsyncTask(MainActivity.this, postData,
new AsyncResponse() {
@Override
public void processFinish(String s) {
try {
if (s.contains("renterowner")) {
Toast.makeText(MainActivity.this, "Renter Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(MainActivity.this, ListActivity.class);
startActivity(in);

} else if (s.contains("ownerrenter")) {
Toast.makeText(MainActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(MainActivity.this, ownerhome.class);
startActivity(in);
} else {
Toast.makeText(MainActivity.this, "Login Failed!", Toast.LENGTH_SHORT).show();
}

}
catch (Exception e) {
System.out.print("Error: " + e.toString());
}
}
});
task1.execute("http://carkila.esy.es/index.php");
}
}


Whenever i type my correct login on my android It always ending up in my else statement but when I'm logging in on my web using
http://carkila.esy.es/index.php
Its working.

database-config.php



<?php
// define database related variables
$database = '*****';
$host = '*****';
$user = '*****';
$pass = '*****';

// try to conncet to database
$dbh = new PDO("mysql:dbname={$database};host={$host}", $user, $pass);
return $dbh;


try{
if(!$dbh){

echo "unable to connect to database";
}
}catch (PDOException $e){
echo $e->getMessage();
}

?>


index.php



<?php

$errors = array(
1=>"Invalid user name or password, Try again",
2=>"Please login to access this area"
);

$error_id = isset($_GET['err']) ? (int)$_GET['err'] : 0;

if ($error_id == 1) {
echo $errors[$error_id];

}elseif ($error_id == 2) {
echo $errors[$error_id];
}
?>

<html>
<head><title>Login</title></head>
<body>

<form action="authenticate.php" method="post">
Username <input type="text" name="username" value="" placeholder="Enter Username" /><br/>
Password <input type="password" name="password" value="" placeholder="Enter Password" /><br/>
<input type="submit" name="btnSubmit" value="Login"/>
</form>
</body>
</html>


authenticate.php



<?php
require 'database-config.php';

session_start();

$username = "";
$password = "";

if(isset($_POST['username'])){
$username = $_POST['username'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];

}


$q = 'SELECT * FROM tbl_user WHERE username=:username AND password=:password';

$query = $dbh->prepare($q);

$query->execute(array(':username' => $username, ':password' => $password));


if($query->rowCount() == 0){
header('Location: index.php?err=1');
}else{

$row = $query->fetch(PDO::FETCH_ASSOC);

session_regenerate_id();
$_SESSION['sess_user_id'] = $row['userID'];
$_SESSION['sess_username'] = $row['username'];
$_SESSION['sess_userrole'] = $row['roles'];

echo $_SESSION['sess_userrole'];
session_write_close();

if( $_SESSION['sess_userrole'] == "renter"){
echo "owner";

}else if ($_SESSION['sess_userrole'] == "owner"){
echo"renter";

}


}


?>

Answer

You are sending the request to the wrong page. Your index.php is not the one responsible for handling POST data, it's authenticate.php.

So, change your code from:

task1.execute("http://carkila.esy.es/index.php");

to:

task1.execute("http://carkila.esy.es/authenticate.php");
Comments