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
    Bonjour j'ai créé un API en laravel pour modifier le profil d'utilisateur et ça marche bien avec Postman, au niveau android j'ai créé une Interface.
    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
     
    public interface UpdateProfile {
     
        String UPDPRO ="http://IP/Projet/public/api/";
        @FormUrlEncoded
        @POST("users/{id}")
        Call<String> UpdateUsers(
                @Path("id") int id,
                @Field("name") String name,
                @Field("adresse") String adresse,
                @Field("prenom") String prenom,
                @Field("email") String email,
                @Field("numtel") String numtel
        );
    }
    et mon profil 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
     
    public class Profile_Fragment extends Fragment  implements View.OnClickListener {
        private EditText editTextName, editTextPrenom,editTextAdresse,editTextEmail,editTextTel;
        private static View view;
        private static Button buttonupdate;
        public Profile_Fragment(){
     
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            view= inflater.inflate(R.layout.profile_fragment, container, false);
            initViews();
            setListeners();
            return view;
            }
        private void initViews() {
            editTextName = view.findViewById(R.id.editTextName);
            editTextPrenom = view.findViewById(R.id.editTextPrenom);
            editTextAdresse=view.findViewById(R.id.editTextAdresse);
            editTextEmail = view.findViewById(R.id.editTextEmail);
            editTextTel = view.findViewById(R.id.editTextTel);
            buttonupdate = view.findViewById(R.id.buttonupdate);
        }
        private void setListeners() {
            buttonupdate.setOnClickListener(this);
        }
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.buttonupdate:
                    updateUsers();
                    break;
            }
        }
        private void updateUsers() {
            int id = getId();
            String name = editTextName.getText().toString().trim();
            String prenom = editTextPrenom.getText().toString().trim();
            String email = editTextEmail.getText().toString().trim();
            String adresse = editTextAdresse.getText().toString().trim();
            String numtel = editTextTel.getText().toString().trim();
     
            if (name.isEmpty()) {
                editTextName.setError("Nom doit être rempli");
                editTextName.requestFocus();
                return;
            }
            if (prenom.isEmpty()) {
                editTextPrenom.setError("Prenom doit être rempli");
                editTextPrenom.requestFocus();
                return;
            }
            if (adresse.isEmpty()) {
                editTextAdresse.setError("Adresse doit être rempli");
                editTextAdresse.requestFocus();
                return;
            }
            if (email.isEmpty()) {
                editTextEmail.setError("Email doit être rempli");
                editTextEmail.requestFocus();
                return;
            }
            if (numtel.isEmpty()) {
                editTextTel.setError("Numero téléphone doit être rempli");
                editTextTel.requestFocus();
                return;
            }
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(UpdateProfile.UPDPRO)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
            UpdateProfile api = retrofit.create(UpdateProfile.class);
            Call<String> call = api.UpdateUsers(id,name,prenom,adresse,email,numtel);
     
            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("status").equals("success")) {
                                    Toast.makeText(getActivity(), "Update Successfully!", Toast.LENGTH_SHORT)
                                            .show();
                                } else if (jsonObject.getString("status").equals("error")) {
                                  /*  new CustomToast().Show_Toast(getActivity(), view,
                                            " " + jsonObject.getString("message"));*/
                                    Toast.makeText(getActivity(), "Update failed!", Toast.LENGTH_SHORT)
                                            .show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
     
                        }
     
                    }
                }
     
                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Toast.makeText(getContext(), "Server invalid \n" + t.getMessage(), LENGTH_LONG).show();
                }
            });
     
     
        }
     
     
    }
    lorsque j’insère des nouveaux informations pour l'utilisateur j'aperçoit Update failed. J'estime que le problème vient du id de l'utilisateur.

  2. #2
    Membre chevronné
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Juillet 2013
    Messages
    269
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2013
    Messages : 269
    Par défaut
    Bonjour,

    à mon avis, tu devrais effectuer des contrôles dans ton API et logger plus précisement ce qui ne va pas.
    Ou utiliser un schema json pour la validation et renvoyer le retour de celle-ci.

Discussions similaires

  1. Retrofit Android Studio
    Par DalyMHY dans le forum Android
    Réponses: 3
    Dernier message: 11/03/2020, 20h19
  2. 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
  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