0

I'm trying to connect an app withe login and register through the use of php this is the code of the android connection part ,the url used ftp to connect the mysql database:

public class BackgroundTask extends AsyncTask<String,Void,String> {

SharedPreferences preferences;
SharedPreferences.Editor editor;

Context context;

BackgroundTask(Context ctx){
    this.context = ctx;
}

@Override
protected String doInBackground(String... params) {

    preferences = context.getSharedPreferences("MYPREFS", Context.MODE_PRIVATE);
    editor = preferences.edit();
    editor.putString("flag","0");
    editor.commit();

    String urlRegistration = "ftp://remoteaddress/site/wwwroot/classes/LoginAndRegister-register.php";
    String urlLogin  = "ftp://remoteaddress/site/wwwroot/classes/LoginAndRegister-login.php";
    String task = params[0];

    if(task.equals("register")){
        String regName = params[1];
        String regEmail = params[2];
        String regPassword = params[3];

        try {
            URL url = new URL(urlRegistration);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            String myData = URLEncoder.encode("identifier_name","UTF-8")+"="+URLEncoder.encode(regName,"UTF-8")+"&"
                    +URLEncoder.encode("identifier_email","UTF-8")+"="+URLEncoder.encode(regEmail,"UTF-8")+"&"
                    +URLEncoder.encode("identifier_password","UTF-8")+"="+URLEncoder.encode(regPassword,"UTF-8");
            bufferedWriter.write(myData);
            bufferedWriter.flush();
            bufferedWriter.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            inputStream.close();

            editor.putString("flag","register");
            editor.commit();
            return "Successfully Registered " + regName;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    if(task.equals("login")){
        String loginEmail = params[1];
        String loginPassword = params[2];
        try {
            URL url = new URL(urlLogin);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            //send the email and password to the database
            OutputStream outputStream = httpURLConnection.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            String myData = URLEncoder.encode("identifier_loginEmail","UTF-8")+"="+URLEncoder.encode(loginEmail,"UTF-8")+"&"
                    +URLEncoder.encode("identifier_loginPassword","UTF-8")+"="+URLEncoder.encode(loginPassword,"UTF-8");
            bufferedWriter.write(myData);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            //get response from the database
            InputStream inputStream = httpURLConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String dataResponse = "";
            String inputLine = "";
            while((inputLine = bufferedReader.readLine()) != null){
                dataResponse += inputLine;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();

            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            System.out.println(dataResponse);

            editor.putString("flag","login");
            editor.commit();
            return  dataResponse;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

//This method willbe called when doInBackground completes... and it will return the completion string which
//will display this toast.
@Override
protected void onPostExecute(String s) {
    String flag = preferences.getString("flag","0");

    if(flag.equals("register")) {
        Toast.makeText(context,s,Toast.LENGTH_LONG).show();
    }
    if(flag.equals("login")){
        String test = "false";
        String name = "";
        String email = "";
        String[] serverResponse = s.split("[,]");
        test = serverResponse[0];
        name = serverResponse[1];
        email = serverResponse[2];

        if(test.equals("true")){
            editor.putString("name",name);
            editor.commit();
            editor.putString("email",email);
            editor.commit();
            Intent intent = new Intent(context,LogginIn.class);
            context.startActivity(intent);
        }else{
            display("Login Failed...", "That email and password do not match our records :(.");
        }
    }else{
        display("Login Failed...","Something weird happened :(.");
    }
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

public void display(String title, String message){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}
}

here is the php connection:

<?
 $hostname = "us-cdbr-azure-southcentral-f.cloudapp.net";
 $username = "XXXXX";
 $dbname = "db";
 $password = "word";

 //Create connection
 $conn = mysql_connect($hostname, $username, $password);
 $selectdb=mysql_select_db($dbname);
 //Check connection
 if(!$conn){
//die("Connectioned failed!" . mysql_connect_error());
}
else{
//echo "Connect success!";
}
?>

and the login.php the password is encrypted so i used sha1()."XXXX" to try to get the password:

<?php
 require "LoginAndRegister-connection.php";

 $user_email = $_POST["identifier_loginEmail"];
 $user_pass =  $_POST["identifier_loginPassword"];


 $mysql_query = "SELECT * FROM usuarios WHERE email = '$user_email'
 AND senha = '$user_pass'";

$result = mysql_query($conn,$mysql_query);

 //check the result
 if(mysql_num_rows($result)>0){
$row = mysql_fetch_assoc($result);
$name = $row["name"];
$email = $row["email"];

//Here is the specially formatted string response.. ie: "ServerResponse".
//It is of the form: "boolean,email,name"
echo "true,".$email.",".$name;
}
else{
echo "Login was not successful... :(";
}

?>

when i run the code this error appears when i try to login in the app :java.lang.ClassCastException: sun.net.www.protocol.ftp.FtpURLConnection cannot be cast to java.net.HttpURLConnection

could someone show where i made the mistakes?

cthulhu
  • 169
  • 14

0 Answers0