Application closes when I try to log in without filling in the fields

Asked

Viewed 154 times

0

I’m developing an application, and I’m not getting the login screen to check if the fields are being filled in or if they have the wrong username and password. Follow the login code.

It logs normal, the same problem is when I click on login without filling the fields or with wrong user or password.

package com.rafaeljacinto.newtest4;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Telalogin extends AppCompatActivity {



    EditText name, password;
    Button login;
    String Name, Password;
    Context ctx=this;
    String codAlu =null, nomeAlu = null,  emailAlu = null, foneAlu = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telalogin);

        name = (EditText) findViewById(R.id.name);
        password = (EditText) findViewById(R.id.password);
        login = (Button)findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Name = name.getText().toString();
                Password = password.getText().toString();
                BackGround b = new BackGround();
                b.execute(Name, Password);

            }
        });
    }

    class BackGround extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            String name = params[0];
            String password = params[1];
            String data="";
            int tmp;

            try {
                URL url = new URL("http://192.168.0.12/ProjetoNewWebService/Login.php");
                String urlParams = "name="+name+"&password="+password;

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                os.write(urlParams.getBytes());
                os.flush();
                os.close();

                InputStream is = httpURLConnection.getInputStream();
                while((tmp=is.read())!=-1){
                    data+= (char)tmp;
                }

                is.close();
                httpURLConnection.disconnect();

                return data;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return "Exception: "+e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "Exception: "+e.getMessage();
            }
        }
        @Override
        protected void onPostExecute(String s) {
            String err=null;


            try {
                JSONObject jo = new JSONObject(s);
                JSONObject dados = jo.getJSONObject("usuario");
                codAlu    = dados.getString("codAlu");
                nomeAlu   = dados.getString("nomeAlu");
                emailAlu  = dados.getString("emailAlu");
                foneAlu   = dados.getString("foneAlu");

            } catch (JSONException e) {
                e.printStackTrace();
                err = "Exception: "+e.getMessage();

            }

            //Toast.makeText(TelaLogin.this, s,Toast.LENGTH_SHORT).show();
            Intent in = new Intent(Telalogin.this,Teladisciplinas.class);
            in.putExtra("codAlu",codAlu);
            in.putExtra("nomeAlu",nomeAlu);
            in.putExtra("emailAlu",emailAlu);
            in.putExtra("foneAlu",foneAlu);
            startActivity(in);

        }
    }
}
  • error is showing what? in android’s logcat stúdio?

1 answer

1


At first just do a validation to check if it has characters typed in the fields name and password. Example:

Name = name.getText().toString();
Password = password.getText().toString();

if(Name.length()>0  && Password.length()>0){              

    BackGround b = new BackGround();
    b.execute(Name, Password);

 } else {

    Toast.makeText(this, "Preencha todos os campos", Toast.LENGTH_SHORT).show();
}

See other ideas:

For the second situation, in which you are entering an incorrect validation, you should check the return within the onPostExecute. Your Intent must be within a condition of success or error when logging in. See below for an example. If your String s is not a JSONObject, this will give you a JSONException.

Example:

 @Override
 protected void onPostExecute(String s) {
    String err=null;

     try {
            JSONObject jo = new JSONObject(s);
            JSONObject dados = jo.getJSONObject("usuario");
            codAlu    = dados.getString("codAlu");
            nomeAlu   = dados.getString("nomeAlu");
            emailAlu  = dados.getString("emailAlu");
            foneAlu   = dados.getString("foneAlu");

            //Toast.makeText(TelaLogin.this, s,Toast.LENGTH_SHORT).show();
            Intent in = new Intent(Telalogin.this,Teladisciplinas.class);
            in.putExtra("codAlu",codAlu);
            in.putExtra("nomeAlu",nomeAlu);
            in.putExtra("emailAlu",emailAlu);
            in.putExtra("foneAlu",foneAlu);
            startActivity(in);

        } catch (JSONException e) {
            e.printStackTrace();
            err = "Exception: "+e.getMessage();

        }
 }
  • 1

    Thank you I will try and then put the result

  • It worked the part of checking if the fields are empty, now the part of typing incorrect data shows no error, simply it enters the application as if the data were validated.

  • @Rafaeljacinto I edited the answer.

  • Ball show worked perfect thank you very much.

  • @Rafaeljacinto legal that worked. If you want to validate the answer, feel free. = D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.