IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Retrofit Android Studio


Sujet :

Android

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2020
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2020
    Messages : 4
    Par défaut Retrofit Android Studio
    Bonsoir à tous, j'ai créé un API en laravel en utilisant passport pour l'authentification, j'ai vérifié le login et ça marche bien
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    {
      "status": "success",
      "data": {
        "token": "eyJ0eXAiOiJKV1QiLCJhbG..."
      }
    }
    au coté mobile, j'ai créé une application mobile et j'ai utilisé Retrofit.
    voici mon LoginInterface
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import retrofit2.Call;
    import retrofit2.http.Field;
    import retrofit2.http.FormUrlEncoded;
    import retrofit2.http.POST;
    public interface LoginInterface {
     
        String LOGINURL = "http://MYIP/AnnocesPFE/public/api/";
        @FormUrlEncoded
        @POST("login")
        Call<String> getUserLogin(
                @Field("email") String email,
                @Field("password") String password
        );
     
    }
    et mon login fragment
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    public class Login_Fragment extends Fragment   implements View.OnClickListener {
     
        private static View view;
     
        private static EditText emailid, password;
        private static Button loginButton;
        private static TextView signUp;
        private static CheckBox show_hide_password;
        private static LinearLayout loginLayout;
        private static Animation shakeAnimation;
        private static FragmentManager fragmentManager;
     
        public Login_Fragment() {
     
        }
     
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.login_layout, container, false);
            initViews();
            setListeners();
            return view;
        }
     
        // Initiate Views
        private void initViews() {
            fragmentManager = getActivity().getSupportFragmentManager();
     
            emailid = (EditText) view.findViewById(R.id.login_emailid);
            password = (EditText) view.findViewById(R.id.login_password);
            loginButton = (Button) view.findViewById(R.id.loginBtn);
            signUp = (TextView) view.findViewById(R.id.createAccount);
            show_hide_password = (CheckBox) view
                    .findViewById(R.id.show_hide_password);
            loginLayout = (LinearLayout) view.findViewById(R.id.login_layout);
     
            // Load ShakeAnimation
            shakeAnimation = AnimationUtils.loadAnimation(getActivity(),
                    R.anim.shake);
     
            // Setting text selector over textviews
            XmlResourceParser xrp = getResources().getXml(R.drawable.text_selector);
            try {
                ColorStateList csl = ColorStateList.createFromXml(getResources(),
                        xrp);
     
     
                show_hide_password.setTextColor(csl);
                signUp.setTextColor(csl);
            } catch (Exception e) {
            }
        }
     
        // Set Listeners
        private void setListeners() {
            loginButton.setOnClickListener(this);
            signUp.setOnClickListener(this);
     
            // Set check listener over checkbox for showing and hiding password
            show_hide_password
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     
                        @Override
                        public void onCheckedChanged(CompoundButton button,
                                                     boolean isChecked) {
     
                            // If it is checkec then show password else hide
                            // password
                            if (isChecked) {
     
                                show_hide_password.setText(R.string.hide_pwd);// change
                                // checkbox
                                // text
     
                                password.setInputType(InputType.TYPE_CLASS_TEXT);
                                password.setTransformationMethod(HideReturnsTransformationMethod
                                        .getInstance());// show password
                            } else {
                                show_hide_password.setText(R.string.show_pwd);// change
                                // checkbox
                                // text
     
                                password.setInputType(InputType.TYPE_CLASS_TEXT
                                        | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                                password.setTransformationMethod(PasswordTransformationMethod
                                        .getInstance());// hide password
     
                            }
     
                        }
                    });
        }
     
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.loginBtn:
                    checkValidation();
                    break;
                // Replace forgot password fragment with animation
                case R.id.createAccount:
                    // Replace signup frgament with animation
                    fragmentManager
                            .beginTransaction()
                            .setCustomAnimations(R.anim.right_enter, R.anim.left_out)
                            .replace(R.id.frameContainer, new SignUp_Fragment(),
                                    Utils.SignUp_Fragment).commit();
                    break;
            }
     
        }
     
        // Check Validation before login
        private void checkValidation() {
            // Get email id and password
            String getEmailId = emailid.getText().toString();
            String getPassword = password.getText().toString();
     
            // Check patter for email id
            Pattern p = Pattern.compile(Utils.regEx);
     
            Matcher m = p.matcher(getEmailId);
     
            // Check for both field is empty or not
            if (getEmailId.equals("") || getEmailId.length() == 0
                    || getPassword.equals("") || getPassword.length() == 0) {
                loginLayout.startAnimation(shakeAnimation);
                new CustomToast().Show_Toast(getActivity(), view,
                        "Enter both credentials.");
     
            }
            // Check if email id is valid or not
            else if (!m.find())
                new CustomToast().Show_Toast(getActivity(), view,
                        "Your Email is Invalid.");
                // Else do login and do your stuff
            else {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(LoginInterface.LOGINURL)
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .build();
                LoginInterface api = retrofit.create(LoginInterface.class);
                Call<String> call = api.getUserLogin(getEmailId, getPassword);
                call.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        if (response.isSuccessful()) {
                            if (response.body() != null) {
                                Log.i("Responsestring", response.body().toString());
                                Log.i("onSuccess", response.body().toString());
     
                                String jsonresponse = response.body().toString();
     
                                try {
                                    JSONObject jsonObject = new JSONObject(jsonresponse);
     
                                    if (jsonObject.getString("success").equals("true")) {
                                        Toast.makeText(getActivity(), "Login Successfully!", Toast.LENGTH_SHORT)
                                                .show();
                                        Intent intent;
                                        intent = new Intent(getActivity(), activity_list_events.class);
                                        startActivity(intent);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
     
                            }
     
                        } else {
                            loginLayout.startAnimation(shakeAnimation);
                            new CustomToast().Show_Toast(getActivity(), view,
                                    "Your Password is Invalid.");
                        }
                    }
     
                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        Toast.makeText(getContext(), "Server invalid \n" + t.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
     
     
            }
        }
    }
    mon MainActivity
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    public class MainActivity extends AppCompatActivity {
     
        private static FragmentManager fragmentManager;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            fragmentManager = getSupportFragmentManager();
     
            // If savedinstnacestate is null then replace login fragment
            if (savedInstanceState == null) {
                fragmentManager
                        .beginTransaction()
                        .replace(R.id.frameContainer, new Login_Fragment(),
                                Utils.Login_Fragment).commit();
            }
     
            // On close icon click finish activity
            findViewById(R.id.close_activity).setOnClickListener(
                    new View.OnClickListener() {
     
                        @Override
                        public void onClick(View arg0) {
                            finish();
     
                        }
                    });
     
        }
     
        // Replace Login Fragment with animation
        protected void replaceLoginFragment() {
            fragmentManager
                    .beginTransaction()
                    .setCustomAnimations(R.anim.left_enter, R.anim.right_out)
                    .replace(R.id.frameContainer, new Login_Fragment(),
                            Utils.Login_Fragment).commit();
        }
     
        @Override
        public void onBackPressed() {
     
            // Find the tag of signup and forgot password fragment
            Fragment SignUp_Fragment = fragmentManager
                    .findFragmentByTag(Utils.SignUp_Fragment);
     
            // Check if both are null or not
            // If both are not null then replace login fragment else do backpressed
            // task
     
            if (SignUp_Fragment != null)
                replaceLoginFragment();
            else
                super.onBackPressed();
        }
    }
    mon problème c'est lors ce que j’insère l'email et le mot de passe qui sont vérifier du l'api quand je clique sur le bouton Login de rien ne ce passe.

  2. #2
    Membre chevronné
    Profil pro
    Inscrit en
    Juillet 2012
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2012
    Messages : 476
    Par défaut
    Bonjour,

    Je ne maîtrise pas trop retrofit mais if faudrait mettre des logs dans tes différents méthodes pour vérifier ce qui est réellement envoyé/reçu...
    Il y a p'tet déjà quelques infos dans la console d'android studio.

    Sinon, ce n'est pas conventionnel d'utiliser le snake case en java. activity_list_events.class devrait être ActivityListEvents.class

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2020
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2020
    Messages : 4
    Par défaut
    Citation Envoyé par Turvy Voir le message
    Bonjour,

    Je ne maîtrise pas trop retrofit mais if faudrait mettre des logs dans tes différents méthodes pour vérifier ce qui est réellement envoyé/reçu...
    Il y a p'tet déjà quelques infos dans la console d'android studio.

    Sinon, ce n'est pas conventionnel d'utiliser le snake case en java. activity_list_events.class devrait être ActivityListEvents.class
    J'ai renommé la classe en ActivtyListEvents et le problème existe encore

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2020
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2020
    Messages : 4
    Par défaut
    Quelqu'un peut m'aider, je suis encore bloqué

Discussions similaires

  1. Google sort un nouvel EDI pour Android : Android Studio
    Par Gordon Fowler dans le forum Android Studio
    Réponses: 43
    Dernier message: 03/12/2013, 19h08
  2. Android Studio encoding problème item de la listview
    Par nadjibnet dans le forum Android Studio
    Réponses: 5
    Dernier message: 02/07/2013, 22h55
  3. Android Studio Git
    Par Twixou dans le forum Android Studio
    Réponses: 1
    Dernier message: 31/05/2013, 08h38
  4. Impossible d'atteindre URI - Android Studio
    Par man_u dans le forum Android Studio
    Réponses: 0
    Dernier message: 23/05/2013, 01h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo