I'm currently making an app under Android which enable me to connect a user. I use regular android server request. I call a php file stored on my server which look if the user entered is currently registered in the BDD, but it's not working at all...
This is my php file :
<?php
$con = mysqli_connect("localhost", "user", "userpass", "Android");
$username = $_POST["username"];
$password = $_POST["password"]
$statement = mysqli_prepare($con, "SELECT * FROM Utilisateur where username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $username, $mail, $password, $adresse, $ville, $zipCode);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user[name] = $name;
$user[username] = $username;
$user[mail] = $mail;
$user[password] = $password;
$user[adresse] = $adresse;
$user[ville] = $ville;
$user[zipCode] = $zipCode;
}
echo json_encode($user);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
This is my request fonction in Java (there is a user:password because you need to authenticate in order to acces any web page on my server)
public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADDRESS = "http://user:userpassword@62.210.193.223/Android/";
public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallBack userCallBack;
public fetchUserDataAsyncTask(User user, GetUserCallBack callBack) {
this.user = user;
this.userCallBack = callBack;
}
@Override
protected User doInBackground(Void... params) {
User returnedUser;
try {
URL url = new URL(SERVER_ADDRESS + "FetchUserData.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
Uri.Builder builder = new Uri.Builder().appendQueryParameter("username", user.username)
.appendQueryParameter("password", user.password);
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = IOUtils.toString(in, "UTF-8");
JSONObject jResponse = new JSONObject(response);
if (jResponse.length() == 0) {
returnedUser = null;
} else {
String name = jResponse.getString("name");
String mail = jResponse.getString("mail");
String adresse = jResponse.getString("adresse");
String ville = jResponse.getString("ville");
int zipCode = jResponse.getInt("zipCode");
returnedUser = new User(name, user.name, user.password, mail, adresse, ville, zipCode);
}
return returnedUser;
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(User returnedUser) {
progressDialog.dismiss();
userCallBack.done(returnedUser);
super.onPostExecute(returnedUser);
}
}
I launched my application in debug mode and I catched this exception :
java.io.FileNotFoundException: http://user:userpassword@62.210.193.223/Android/FetchUserData.php
I even tried to C/C the url in my web browser and it execute the file. Of course it returned [] because I did not include POST in my call.
I'm completly lost because android is quite new to me and I dont really know where to look to find a solution to this problem... If someone could help me to solve this problem it would be very much appreciated :)
Cheers, Astrus