Login Authentication / Jsonobject

Asked

Viewed 173 times

0

I have a get code that takes the email and password from the database.. But in getString it only compares with a term,as I do to use equals with all bank users and not filter by ID... This is 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();
        }

    }
}

}

1 answer

1

Hello,

Voce returned a Jsonarray but this only using the object in Dice 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();
        }
    }
  • Thank you very much ,it is apparently returning an error in line "for(Jsonobject jsonObj : array){",no array ...foreach not applicable to type 'org.Jsonarray',but thank you !!

  • Hello Father, I’ve adjusted the code above.... it seems that Jsonarray does not work with foreach, as I have no way to test your code anything from one touch;)

Browser other questions tagged

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