1

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

Astrus
  • 173
  • 14
  • i think `"http://user:userpassword@62.210.193.223/Android/"` is not a valid server address.. try to use `"http://62.210.193.223/Android/"` – ELITE Feb 26 '16 at 13:26
  • My suggestion is to check if url is online or not before going to call in android..try with any browse if its working proper. – ELITE Feb 26 '16 at 13:28
  • I tried and I obtained the exact same problem :/ – Astrus Feb 26 '16 at 13:28
  • check if it works on browser..if not then thats the problem, link is not online – ELITE Feb 26 '16 at 13:29
  • Like I said in my request, I have tried in my regular web Browser ELITE and its working fine... – Astrus Feb 26 '16 at 13:29
  • add `conn.setDoInput(true); conn.setDoOutput(true);` after `conn.setRequestMethod("POST");` and remove line `conn.connect();` – ELITE Feb 26 '16 at 13:31
  • I have changed the input as you said and I ran in debug mode. I catched the same exact exception : java.io.FileNotFoundException: http://62.210.193.223/Android/FetchUserData.php By the way, thank you for trying to help me it's much appreciated :) – Astrus Feb 26 '16 at 13:40
  • I need to execute the php file on the server and get the output of the request. You're suggestion it's for downloading a file from a server I think it will not help no ? – Astrus Feb 26 '16 at 13:45

1 Answers1

0

It seems you are using BasicAuth for HTTP security. Providing username and password is not the good way to provide Basic credentials for your request.

You need to set the correct Authorization header :

URLConnection uc = url.openConnection();
String userpass = username + ":" + password;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty ("Authorization", basicAuth);

See : Connecting to remote URL which requires authentication using Java

Community
  • 1
  • 1
Gut
  • 291
  • 1
  • 10
  • Thank you for your answer. I'm pretty new to Android so it's not completly clear to me yet. Should I add this code next to my : URL url = new URL(SERVER_ADDRESS + "FetchUserData.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); Or should I replace the code ? Astrus – Astrus Feb 26 '16 at 17:11
  • You're welcome. If this is the good answer, please validate it to close your question. – Gut Feb 28 '16 at 08:18
  • You just need to remove user name and password from SERVER_ADDRESS and put it in your code just after conn.setRequestMethod("POST"); I have not tried but other parts must be OK – Gut Feb 28 '16 at 08:21