mardi 21 juillet 2015

Android app using SQLite login - registeration not working properly

i have implemented a login - registration database storing and retrieval on an android app using OpenConnection().

i HAVE DONE EVERYTHING PERFECTLY ACCORDING TO ME BUT I AM FACING MORE THAN ONE VERY UNEXPECTED ISSUES..

1) App says the login is successful even when the user and password is not correct. ( even on empty username and password)

2) The app says the login is successful the first time but when i try to sign in again with a correct username and password it executes the if statement which is supposed to run when the user password is not correct..and takes you back to sign in activity..

I am uploading my signin.java , backgroundtask.java and my finallogin.java ( the activity where it checks the login is successfull or not )

I am also provising my Logcat below :

Signin.java :

public class Signin extends Activity {

TextView signuplink;
EditText signusername , signpassword;

Button signin;
String login_name;
String login_pass;

@Override
protected void onCreate(Bundle savedInstanceState) {


    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);


        setContentView(R.layout.signin);
        signusername = (EditText) findViewById(R.id.susername);
        signpassword = (EditText) findViewById (R.id.spassword);




    signin = (Button) findViewById(R.id.signinbutt);
    signin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            userlogin(v);
            Intent finallogin = new Intent ("com.example.eightmiles.FINALLOGIN");
            startActivity(finallogin);



        }
    });

    signuplink = (TextView) findViewById(R.id.signuplink);
    signuplink.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent signuppage = new Intent ("com.example.eightmiles.SIGNUP");
            startActivity(signuppage);
        }
    });

}

public void userlogin(View view)
{
    login_name = signusername.getText().toString();
    login_pass = signpassword.getText().toString();

    String method = "login";
    Backgroundtask backgroundtask = new Backgroundtask(this);
    backgroundtask.execute(method,login_name,login_pass);

}

}

Backgroundtask.java :

public class Backgroundtask extends AsyncTask<String,Void,String> {  
  AlertDialog alertDialog;  
  AlertDialog alertDialog2;  
   Context ctx;  
   Userlocalstore userlocalstore;
   String res_name , res_username , res_dob , res_email , res_id , res_pass;
   Backgroundtask(Context ctx)  
   {  
    this.ctx =ctx;  
   }  
   @Override  
   protected void onPreExecute() {  
   alertDialog = new AlertDialog.Builder(ctx).create();  
     alertDialog.setTitle("Login Information....");  
     alertDialog2 = new AlertDialog.Builder(ctx).create();  
     alertDialog2.setTitle("Apologies");  
   }  
   @Override  
   protected String doInBackground(String... params) {  
     String reg_url = "http://ift.tt/1HtAfgU";  
     String login_url = "http://ift.tt/1IdZ6XB";  
     String method = params[0];  
     if (method.equals("Register")) {  
       String name = params[1];  
       String user_name = params[2];  
       String user_pass = params[3];  
       String email = params[4];  
       String dob = params[5];  

       try {  
         URL url = new URL(reg_url);  
         HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();  
         httpURLConnection.setRequestMethod("POST");  
         httpURLConnection.setDoOutput(true);  
         //httpURLConnection.setDoInput(true);  
         OutputStream OS = httpURLConnection.getOutputStream();  
         BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));  
         String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +  
             URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +  
             URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8") + "&" + 
             URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" + 
             URLEncoder.encode("dob", "UTF-8") + "=" + URLEncoder.encode(dob, "UTF-8");  
         bufferedWriter.write(data);  
         bufferedWriter.flush();  
         bufferedWriter.close();  
         OS.close();  
         InputStream IS = httpURLConnection.getInputStream();  
         IS.close();  
         //httpURLConnection.connect();  
         httpURLConnection.disconnect();  
         return "Registration Success...";  
       } catch (MalformedURLException e) {  
         e.printStackTrace();  
       } catch (IOException e) {  
         e.printStackTrace();  
       }  
     }  
     else if(method.equals("login"))  
     {  
       String login_name = params[1];  
       String login_pass = params[2];  
       try {  
         URL url = new URL(login_url);  
         HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();  
         httpURLConnection.setRequestMethod("POST");  
         httpURLConnection.setDoOutput(true);  
         httpURLConnection.setDoInput(true);  
         OutputStream outputStream = httpURLConnection.getOutputStream();  
         BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));  
       String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+  
           URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");  
         bufferedWriter.write(data);  
         bufferedWriter.flush();  
         bufferedWriter.close();  
         outputStream.close();  
         InputStream inputStream = httpURLConnection.getInputStream();  
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));  
         String response = "";  
         String line = "";  
         while ((line = bufferedReader.readLine())!=null)  
         {  
           response += line;  
         }

         StringTokenizer tokens = new StringTokenizer(response, ",");
          String res_username = tokens.nextToken();

          if(res_username != "failed")
          {
         String res_name = tokens.nextToken();
         String res_dob = tokens.nextToken();
         String res_email = tokens.nextToken();
         String res_id = tokens.nextToken();
         String res_pass = " ";


          user user = new user(res_name,res_username,res_pass,res_email,res_dob,res_id);
          Userlocalstore userlocalstore = new Userlocalstore(ctx);
          userlocalstore.storeuserdata(user);
          userlocalstore.setuserloggedin(true);
          }



         bufferedReader.close();  
         inputStream.close();  
         httpURLConnection.disconnect();  
         return response;  
       } catch (MalformedURLException e) {  
         e.printStackTrace();  
       } catch (IOException e) {  
         e.printStackTrace();  
       }  
     }  
     return null;  
   }  
   @Override  
   protected void onProgressUpdate(Void... values) {  
     super.onProgressUpdate(values);  
   }  
   @Override  
   protected void onPostExecute(String result) {  

       StringTokenizer tokens = new StringTokenizer(result, ",");
          String res_username = tokens.nextToken();

    if(result.equals("Registration Success..."))  
    {  
      Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();  
    }  
     else  
    {  

    }
 }

}

Finallogin.java :

package com.example.eightmiles;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Finallogin extends Activity{

    Button logout;
    Userlocalstore userlocalstore;
    TextView success;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finallogin);
        userlocalstore = new Userlocalstore(this);
        logout = (Button) findViewById(R.id.logoutbutt);
        success = (TextView) findViewById(R.id.successtext);

        logout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                userlocalstore.clearuserdata();
                userlocalstore.setuserloggedin(false);
                Intent logout = new Intent ("com.example.eightmiles.SIGNIN");
                startActivity(logout);
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        boolean bool = authenticate();
        if(bool == true)
        {
            success.setText("Login Successful");
        }
        else if(bool == false)
        {
            Intent signinpage = new Intent ("com.example.eightmiles.SIGNIN");
            startActivity(signinpage);
        }

    }

    private boolean authenticate()
    {
        return userlocalstore.getbooluserloggedin();


    }

}

My logcat:

package com.example.eightmiles;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Finallogin extends Activity{

    Button logout;
    Userlocalstore userlocalstore;
    TextView success;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finallogin);
        userlocalstore = new Userlocalstore(this);
        logout = (Button) findViewById(R.id.logoutbutt);
        success = (TextView) findViewById(R.id.successtext);

        logout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                userlocalstore.clearuserdata();
                userlocalstore.setuserloggedin(false);
                Intent logout = new Intent ("com.example.eightmiles.SIGNIN");
                startActivity(logout);
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        boolean bool = authenticate();
        if(bool == true)
        {
            success.setText("Login Successful");
        }
        else if(bool == false)
        {
            Intent signinpage = new Intent ("com.example.eightmiles.SIGNIN");
            startActivity(signinpage);
        }

    }

    private boolean authenticate()
    {
        return userlocalstore.getbooluserloggedin();


    }

}

Aucun commentaire:

Enregistrer un commentaire