1

I'm having problem with redirecting user to new page after the login authentication. If I try to direct my next page in postexecute method it works with Intent home = new Intent(context,Home_page.class); context.startActivity(home); but then it directs it to next page even if the login credentials are wrong. How do i modify my code so that it first check the login credentials and then links the next page with login button.

class background_login extends AsyncTask<String, Void,String>
{

    AlertDialog dialog;
    Context context;
    public background_login (Context context)
    {
        this.context = context;
    }


    @Override
    protected void onPreExecute() {
        dialog = new AlertDialog.Builder(context).create();
        dialog.setTitle("Login Status");

    }

    @Override
    protected void onPostExecute(String s) {
        dialog.setMessage(s);
        dialog.show();



    }

    @Override
    protected String doInBackground(String... params)
    {
        String results="";
        String user_name = params[0];
        String password = params[1];

        String connstr= "url";

        try
        {
            URL url= new URL(connstr);
            HttpURLConnection http =(HttpURLConnection) url.openConnection();
            http.setRequestMethod("POST");
            http.setDoInput(true);
            http.setDoOutput(true);

            OutputStream ops = http.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops,"UTF-8" ));
            String data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")
                    +"&&"+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");

            writer.write(data);
            writer.flush();
            writer.close();
            ops.close();


            InputStream ips = http.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(ips, "ISO-8859-1"));
            String line ="";
            while ((line = reader.readLine()) !=null)
            {
                results+=line;
            }

            reader.close();
            ips.close();
            http.disconnect();
            return results;






        } catch (MalformedURLException e) {
            results= e.getMessage();
        } catch (IOException e) {
            results=e.getMessage();
        }


        return results;
    }
}
  • Does this answer your question? [Redirect after android login activity](https://stackoverflow.com/questions/26670227/redirect-after-android-login-activity) – hasan.alkhatib May 20 '20 at 07:36
  • actually no as i can't match my username and password with "admin" as he did in his code. I have multiple users in my app. So how i can match the values with database and direct it to another page? – Rachit Sharma May 20 '20 at 19:51

1 Answers1

0

In onPostExecute you should check request response data like json, xml etc. and than call specific activity or provide error message. AsyncTask json example: Get JSON in AsyncTask Android

@Override
protected void onPostExecute(JSONObject jsonData){
   try {
        boolean login = jsonData.getBoolean('isLoginSuccess');
        if(login){
                  String keyToAccessLater = jsonData.getString('accessKey');
                  Intent home = new Intent(((Activity)context), Home_page.class); 
                  ((Activity)context).startActivity(home); 
        else
            ...
   ...
}
MARKODY
  • 9
  • 1
  • 2