Bonjour à tous !

Je débute sur Androïd, j'ai commencé par un tutoriel mais, il y a une chose qui est obscure et qui n'est pas claire au niveau des différents tutos que j'ai lu.

Ma question est simple, comment envoyer une liste contenant des objets d'une classe avec retrofit à un web service distant ? Dans les différents tutoriels, j'ai vue comment envoyer une variable ou un objet mais impossible de trouver pour une liste.

Voici ce que j'ai fait pour l'instant :

Ma classe d'objets :
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
public class Appel {
    @Expose
    private String Id_appel;
    @Expose
    private String Date_appel;
    @Expose
    private String Duree_appel;
    @Expose
    private String Number_appel;
    @Expose
    private String Type_appel;
 
    public void Appel(){
        Id_appel="";
        Date_appel="";
        Duree_appel="";
        Number_appel="";
        Type_appel="";
    }
 
    public void setIdAppel (String Id_appel){
        this.Id_appel = Id_appel;
    }
    public void setDateAppel (String Date_appel){
        this.Date_appel = Date_appel;
    }
    public void setDureeAppel (String Duree_appel){
        this.Duree_appel = Duree_appel;
    }
    public void setNumberAppel (String Number_appel){
        this.Number_appel = Number_appel;
    }
    public void setTypeAppel (String Type_appel){
        this.Type_appel = Type_appel;
    }
 
 
    public String getIdAppel (){
        return Id_appel;
    }
    public String getDateAppel (){
        return Date_appel;
    }
    public String getDureeAppel (){
        return Duree_appel;
    }
    public String getNumberAppel (){
        return Number_appel;
    }
    public String getTypeAppel (){
        return Type_appel;
    }
 
}
Mon interface avec le post :
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
package abw.ctismartphone.service;
 
import java.util.List;
 
import abw.ctismartphone.models.Appel;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
 
/**
 * Created by JFANTOU on 21/12/2016.
 */
 
public interface APIService {
    @FormUrlEncoded
    @POST
    Call<List<Appel>> sendListeAppel(@Body List<Appel> appel);
}
Et l'activité où je récupère ma liste d'appel pour ensuite faire le post :

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
package abw.ctismartphone;
 
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.CallLog;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
import java.lang.*;
import java.util.Map;
 
import abw.ctismartphone.models.Appel;
import abw.ctismartphone.service.APIService;
import abw.ctismartphone.service.RetrofitBuilder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.MoshiConverterFactory;
import retrofit2.Response;
import retrofit2.Retrofit;
 
public class MainActivity extends Activity {
    protected ListView mListView;
    private Handler myHandler;
    private List<String> ListeNumero;
    private List<String> ListeNumeroGroupe;
    private List<Appel> ListeAppel;
    private ArrayAdapter<String>  adapter;
    private Runnable myRunnable = new Runnable() {
        public void run() {
    // Code à éxécuter de façon périodique
            mListView = (ListView) findViewById(R.id.listView);
            ListeNumero.clear();
            ListeAppel.clear();
            Appel appel = new Appel();
            int i=0;
 
            Uri allCalls = Uri.parse("content://call_log/calls");
            Cursor c = managedQuery(allCalls, null, null, null, null);
            //Récupération de l'historique
            try {
                while (c.moveToNext()) {
                    String number = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
                    appel.setNumberAppel(number);
                    String type = c.getString(c.getColumnIndex(CallLog.Calls.TYPE));
                    appel.setTypeAppel(type);
                    String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
                    appel.setDureeAppel(duration);
                    String date = c.getString(c.getColumnIndex(CallLog.Calls.DATE));
                    appel.setDateAppel(date);
                    String idAppel = c.getString(c.getColumnIndex(CallLog.Calls._ID));
                    appel.setIdAppel(idAppel);
                    //Liste des appels qui sera envoyé au serveur pour enregistrer les informations
                    ListeAppel.add(appel);
                    //Liste qui contient les appels pour ensuite être traité afin d'avoir le nombre d'enregistrement par occurence
                    ListeNumero.add(i,number);
                    i++;
                }
                countElemenetsLists(ListeNumero);
                mListView.setAdapter(adapter);
 
                //On envoie les données au serveurs
 
                myHandler.postDelayed(this,60000);
            } finally {
                c.close();
            }
 
            //Envoie des données
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://api.github.com")
                    .addConverterFactory(MoshiConverterFactory.create())
                    .build();
 
            APIService service = retrofit.create(APIService.class);
            Call<List<Appel>> call = service.sendListeAppel(ListeAppel);
 
            call.enqueue(new Callback<List<Appel>>() {
                @Override
                public void onResponse(Response<List<Appel>> response, Retrofit retrofit) {
                    Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
 
                }
 
                @Override
                public void onFailure(Throwable t) {
                    Toast.makeText(MainActivity.this, "KO", Toast.LENGTH_SHORT).show();
 
                }
            });
        }
    };
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListeNumero = new ArrayList<String>();
        ListeNumeroGroupe = new ArrayList<String>();
        ListeAppel = new ArrayList<Appel>();
        adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1, ListeNumeroGroupe);
        setContentView(R.layout.activity_main);
        myHandler = new Handler();
        myHandler.postDelayed(myRunnable,60000);
    }
 
    public void onPause() {
        super.onPause();
        if(myHandler != null)
            myHandler.removeCallbacks(myRunnable); // On arrete le callback
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        if(myHandler != null)
            myHandler.post(myRunnable);
    }
 
    @Override
    protected void onRestart() {
        super.onResume();
        if(myHandler != null)
            myHandler.post(myRunnable);
    }
 
    @Override
    protected void onStart() {
        super.onResume();
        if(myHandler != null)
            myHandler.postDelayed(myRunnable,60000);
    }
 
    private void countElemenetsLists (List ListeNumero) {
        Map<String, Integer> mp = new HashMap<String, Integer>();
        String num = null;
        ListeNumeroGroupe.clear();
        for (int i = 0; i < ListeNumero.size(); i++) {
            num = (String) ListeNumero.get(i);
            if (num != null) {
                if (mp.keySet().contains(num)) {
                    mp.put(num, mp.get(num) + 1);
 
                } else {
                    mp.put(num, 1);
                }
            }
        }
        for(Map.Entry<String, Integer> entry : mp.entrySet()) {
            ListeNumeroGroupe.add(entry.getKey() + " (" + entry.getValue() +")");
        }
    }
 
    public void sendHistoricCall(List<Appel> ListeAppel) {
    }
}