Login Authentication / JsonObject

0
  

I have a get code that gets the email and password from the database. But in getString it only compares to a term, how do I use equals with all users of the database and do not filter by ID. . That's my code:

private class UserLoginTask extends AsyncTask {

    private final String mEmail;
    private final String mPassword;
    //private final String mCep;

    UserLoginTask(String email, String password) {
        mEmail = email;
        mPassword = password;


    }

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

        HttpURLConnection httpCon = null;

        StringBuilder result = new StringBuilder();
        //  Toast.makeText(LoginActivity.this,"No começo de doInbackground....",Toast.LENGTH_LONG).show();

     //   int id = 1;

        try {

            String urlLogin = "UrlApi";


            URL url = new URL(urlLogin);
            httpCon = (HttpURLConnection) url.openConnection();


            httpCon.setRequestProperty("Accept", "application/json");
            httpCon.setRequestMethod("GET");
            httpCon.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            httpCon.setRequestProperty("X-DreamFactory-Session-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.yWiWTAhyvZgT1ROUDSb4S0Bc1DQFbVbqbMbJ868EfKw");
            httpCon.setRequestProperty("Authorization", "Basic  dGhpYWdvLmNhbWFyZ29AZXZvbHV0aW9uaXQuY29tLmJyOmluaWNpYWwyMDE3");



            InputStream in = new BufferedInputStream(httpCon.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            httpCon.disconnect();
        }
        System.out.println(result.toString());

        return result.toString();
    }

    @Override
    protected void onPostExecute(final String result) {

        super.onPostExecute(result);

        JSONObject json = null;
        try {
            json = new JSONObject(result);
            System.out.println(json.getString("resource"));

            JSONArray array = new JSONArray(json.getString("resource"));




            System.out.println("Email : "+ array.getJSONObject(1).getString("email"));
            System.out.println("Password : "+ array.getJSONObject(1).getString("password"));
            System.out.println("email :"+ array.getJSONObject(1.getString("email"));

            String email = array.getJSONObject(1).getString("email");
            String password = array.getJSONObject(1).getString("password");
            if (mEmail.equals(email) && mPassword.equals(password))
            {
                Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
                intent.putExtra("result", result);
                startActivity(intent);
            }

            else {
                Toast.makeText(LoginActivity.this,"Email ou senha inválido(s)",Toast.LENGTH_LONG).show();


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

}

    
asked by anonymous 15.05.2017 / 21:34

1 answer

1

Hello,

You have returned a JSONArray but you are only using the object at index 1 in the comparison, try this way inside the try:

json = new JSONObject(result);
    System.out.println(json.getString("resource"));
    JSONArray array = new JSONArray(json.getString("resource"));
    for (int i = 0; i < array.length(); i++) {
        JSONObject jsonObj = array.getJSONObject(i);
        System.out.println("Email : "+ jsonObj.getString("email"));
        System.out.println("Password : "+ jsonObj.getString("password"));
        System.out.println("email :"+ jsonObj.getString("email"));
        String email = jsonObj.getString("email");
        String password = jsonObj.getString("password");
        if (mEmail.equals(email) && mPassword.equals(password))
        {
            Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
            intent.putExtra("result", result);
            startActivity(intent);
            return;
        }
        else {
            Toast.makeText(LoginActivity.this,"Email ou senha inválido(s)",Toast.LENGTH_LONG).show();
        }
    }
    
15.05.2017 / 22:33