Bonjour,
Je suis actuellement entrain de développer une application pour mes études et j'ai un petit soucis de "transfert de variable".
Je m'explique:
- J'ai une classe ConnectCoreActivity qui est une activité de mon application
- Depuis cette activité je créer un objet myClient
- Client est une classe que j'ai récupéré sur internet, permettant de communiquer par socket
- J'exécute myClient
- La connexion s'établie bien, le serveur me renvoie une réponse
- Cette réponse s'affiche dans une textView grâce à la fonction .setText exécuté directement dans la classe Client
- La réponse s'affiche correctement sur mon téléphone
Mon soucis maintenant, c'est que j'aimerais récupérer ce texte et le rentrer dans une variable String, afin de pouvoir la traiter.
Et là... C'est la drame... Après divers tests, rien, impossible de récupérer ce texte... :
Je pense que le soucis viens du fais que ConnectCoreActivity soit hérité de Activity et que Client soit une AsyncTask...
Est-ce que quelqu'un aurait une solution propre à me proposer pour régler ce problème minim, mais qui m'a déjà fait perdre beaucoup de temps ?
Voici les 2 classes:
ConnectCoreActivity.java:
Client.java:
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 package com.example.bouhh.mys; import android.content.Intent; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class ConnectCoreActivity extends Activity { TextView response; EditText editTextAddress, editTextPort; Button buttonConnect, buttonClear, buttonNext; String reponse = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.connect_core); editTextAddress = (EditText) findViewById(R.id.addressEditText); editTextPort = (EditText) findViewById(R.id.portEditText); buttonConnect = (Button) findViewById(R.id.connectButton); buttonClear = (Button) findViewById(R.id.clearButton); response = (TextView) findViewById(R.id.responseTextView); buttonNext = (Button) findViewById(R.id.nextButton); /* buttonConnect.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Client myClient = new Client(editTextAddress.getText() .toString(), Integer.parseInt(editTextPort .getText().toString()), response, "test1"); myClient.execute(); } }); */ buttonNext.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent myIntent = new Intent(ConnectCoreActivity.this, LoginActivity.class); startActivity(myIntent); } }); buttonConnect.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Client myClient = new Client("192.168.43.111", 4242 , response, "INT&000&C6A&TEST&null"); myClient.execute(); } }); buttonClear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { response.setText(""); } }); } public void setReponse(String reponse) { this.reponse = reponse; } public String getReponse() { return reponse; } }
Je vous remercie !
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 package com.example.bouhh.mys; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; import java.net.UnknownHostException; import android.content.Context; import android.os.AsyncTask; import android.util.Log; import android.widget.TextView; import java.io.PrintWriter; public class Client extends AsyncTask<Void, Void, Void> { String dstAddress; int dstPort; String response = "je suis null"; TextView textResponse; Socket socket = null; String message = "Message vide"; Client(String addr, int port, TextView textResponse, String mes) { dstAddress = addr; dstPort = port; this.textResponse=textResponse; message = mes; } @Override protected Void doInBackground(Void... arg0) { try { socket = new Socket(dstAddress, dstPort); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( 1024); byte[] buffer = new byte[1024]; int bytesRead; InputStream inputStream = socket.getInputStream(); Send_Message(message); /* * notice: inputStream.read() will block if no data return */ while ((bytesRead = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, bytesRead); response += byteArrayOutputStream.toString("UTF-8"); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); response = "UnknownHostException: " + e.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); response = "IOException: " + e.toString(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; } @Override protected void onPostExecute(Void result) { textResponse.setText(response); super.onPostExecute(result); } protected void Send_Message (String message){ try { PrintWriter writer = new PrintWriter(socket.getOutputStream(),true); writer.write(message); writer.flush(); } catch (IOException e) { e.printStackTrace(); } } }
Julien.
Partager