Android Studio Retrofit
DalyMHY
Messages postés
6
Date d'inscription
Statut
Membre
Dernière intervention
-
BunoCS Messages postés 15952 Date d'inscription Statut Modérateur Dernière intervention -
BunoCS Messages postés 15952 Date d'inscription Statut Modérateur Dernière intervention -
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
et mon profil fragment
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, puisque quand je change la methode PUT par PUT('users/2') par exemple je donnes le id moi même ça fonctionne bien.
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
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
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, puisque quand je change la methode PUT par PUT('users/2') par exemple je donnes le id moi même ça fonctionne bien.
A voir également:
- Android Studio Retrofit
- Android recovery - Guide
- Telecharger fl studio 20 pour pc gratuit complet - Télécharger - Édition & Montage
- Begone android - Accueil - Protection
- À quoi sert google drive sur android - Guide
- Historique presse-papier android - Guide
Sinon, tu print le log et tu regardes la fenêtre Logcat.
J'ai une petite question lors de la connexion dans mon application j’obtiens une reponse json avec tous les détails du utilisateur connecté {"status":"success","data":{"id":2,"name":"user","prenom":"user"...} est ce que je peux utiliser l'id de ce utilisateur et de l'utiliser dans cette fragment pour résolu ce problème ?