Bonjour,
je souhaite créer un programme "quiz" qui envoie des questions et réponses par socket, le serveur est en java et le client en android, à l'aide d'une AsyncTask j'arrive à me connecter au serveur et récupérer une chaîne de caractère envoyer par le serveur pour remplir automatiquement mes TextView ou RadioButton, mais j'arrive seulement à en récupérer une seule alors que je voudrais en récupérer plusieurs. Comment modifier ma fonction pour que j'arrive à prendre toutes les chaines de caractères envoyées ?
Voici mon code :
Coté serveur :
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 public class serveurtest { public static void main(String[] args){ // Scanner sc = new Scanner(System.in); int b = 0; ServerSocket socketserver; Socket socketduserveur ; BufferedReader in = null ; PrintWriter out; String rep; try { socketserver = new ServerSocket(2009); InetAddress address = InetAddress.getLocalHost(); String hostIP = address.getHostAddress() ; String hostName = address.getHostName(); System.out.println( "Le nom de serveur est : " + hostName + "\nIP: " + hostIP); System.out.println("Le serveur est à l'écoute du port "+socketserver.getLocalPort()); socketduserveur = socketserver.accept(); System.out.println("client connecté"); in = new BufferedReader(new InputStreamReader(socketduserveur.getInputStream())); out = new PrintWriter(socketduserveur.getOutputStream()); out.println("Question"); out.flush(); out.println("Réponse 1"); out.flush(); out.println("Réponse 2"); out.flush(); out.println("Réponse 3"); out.flush(); out.println("Réponse 4"); out.flush(); rep = in.readLine(); socketduserveur.close(); socketserver.close(); } catch (Exception e) { e.printStackTrace(); System.out.println("erreur 1"); } } }
Coté client :
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 public class main extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.qcm); new ConnectionTask().execute(); } class ConnectionTask extends AsyncTask<Void,Void,String> { public String doInBackground(Void...params) { String ret = null; BufferedReader in = null; PrintWriter out = null; Socket socket =null ; try { socket = new Socket("192.168.10.104",2009); //adresse IP actuelle du serveur in = new BufferedReader (new InputStreamReader (socket.getInputStream())); out = new PrintWriter(socket.getOutputStream()); out.println("coucou"); ret = in.readLine(); } catch (Exception ex) { // on utilise Log sous android ! Log.e("ConnectionTask","Failure !0",ex); } finally { try { if (out != null) out.close(); if (in != null) in.close(); socket.close(); } catch (Exception ex1) { Log.e("ConnectionTask","Failure !1",ex1); } } return ret; } // ici on est à nouveau dans le thread "UI" on a donc le droit de modifier celle-ci public void onPostExecute(String result) { if (result != null) { TextView tv = (TextView) main.this.findViewById(R.id.test); tv.setText(result); } } } }
et le xml, qcm.xml :
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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checkedButton="@+id/rep1" android:orientation="vertical" android:paddingTop="20dp" > <RadioButton android:id="@+id/rep1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rep2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rep3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rep4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> <TextView android:id="@+id/test" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#C60800" android:textSize="20sp" /> </LinearLayout>
Merci d'avance !
Partager