Enviar valor de variable a MainActivity - Android
Publicado por Luciano (1 intervención) el 13/06/2019 03:31:07
Hola gente buenas noches. Estoy teniendo un problema con una APP de Android.
Estoy usando el protocolo TCP para comunicarme con la PC que uso de servidor, cuando le envió un valor al servidor, me responde y esa respuesta la almaceno en una clase pero al momento de querer ver el valor de esa variable desde el MainActivity, no puedo (esta null)
Les dejo el código para ver si pueden darme una mano por favor.
Esa es mi Clase "Client" donde en el metodo DoInBackground recibo la respuesta y la almaceno en la variable "validacion" pero como dije anteriormente, cuando llamo a esa variable desde el MainActivity, esta NULL.
Les dejo el codigo del MainActivity
Gracias!
Estoy usando el protocolo TCP para comunicarme con la PC que uso de servidor, cuando le envió un valor al servidor, me responde y esa respuesta la almaceno en una clase pero al momento de querer ver el valor de esa variable desde el MainActivity, no puedo (esta null)
Les dejo el código para ver si pueden darme una mano por favor.
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
public class Client {
private static final String TAG = Client.class.getSimpleName();
private Socket socket;
private PrintWriter out;
private boolean connected;
public String respuestaServidor;
public static String validacion;
MainActivity prueba = new MainActivity();
public Client()
{
socket = null;
out = null;
connected = false;
}
public void connect(Context context, String host, int port, String trama)
{
new ConnectTask(context).execute(host, String.valueOf(port), trama);
}
private class ConnectTask extends AsyncTask<String, Void, Void> {
private Context context;
public ConnectTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
showToast(context, "Conectando..");
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
if (connected) {
showToast(context, "Se ha conectado!");
}
super.onPostExecute(result);
}
private String host;
private int port;
@Override
protected Void doInBackground(String... params) {
try {
String[] partee;
String host = params[0];
int port = Integer.parseInt(params[1]);
String trama = params[2];
socket = new Socket(host, port);
out = new PrintWriter(socket.getOutputStream(), true);
out.println(trama); //ENVIO LA TRAMA AL SERVIDOR
// RECIBO RESPUESTA DEL SERVIDOR
DataInputStream input = new DataInputStream(socket.getInputStream());
respuestaServidor = input.readLine(); //GUARDO LA RESPUESTA
System.out.println("Recibiendo....." + respuestaServidor);
// FIN RECIBIR RESPUESTA DEL SERVIDOR
// DIVIDO LA RESPUESTA EN PARTES
partee = respuestaServidor.split("\\|");
validacion = partee[1]; //GUARDO LA PARTE QUE NECESITO
System.out.println("a ver: " + validacion);
// FIN DIVIDIR RESPUESTA EN PARTES
} catch (UnknownHostException e) {
showToast(context, "Don't know about host: " + host + ":" + port);
Log.e(TAG, e.getMessage());
} catch (IOException e) {
showToast(context, "Couldn't get I/O for the connection to: " + host + ":" + port);
Log.e(TAG, e.getMessage());
}
connected = true;
return null;
}
}
public void disconnect(Context context)
{
if ( connected )
{
try {
out.close();
socket.close();
connected = false;
} catch (IOException e) {
showToast(context, "Couldn't get I/O for the connection");
Log.e(TAG, e.getMessage());
}
}
}
/**
* Send command to a Pure Data audio engine.
*/
public void send(String command)
{
if ( connected ) out.println(command +";");
}
private void showToast(final Context context, final String message) {
new Handler(context.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
});
}
}
Esa es mi Clase "Client" donde en el metodo DoInBackground recibo la respuesta y la almaceno en la variable "validacion" pero como dije anteriormente, cuando llamo a esa variable desde el MainActivity, esta NULL.
Les dejo el codigo del MainActivity
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
Client client;
String trama = ""; //GUARDO LA TRAMA
String fecha = ""; //GUARDO FECHA ACTUAL
@Override
protected void onCreate(Bundle savedInstanceState) {
client = new Client();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//LE DOY TRANSPARENCIA AL LOGO
ImageView img = (ImageView) findViewById(R.id.logo);
img.setAlpha(10);
//FIN TRANSPARENCIA
//OBTENGO FECHA
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
Date date = new Date();
fecha = dateFormat.format(date);
//FIN OBTENER FECHA
Toast.makeText(getApplicationContext(), fecha, Toast.LENGTH_LONG).show();
final TextView dni = (TextView) findViewById(R.id.txtDni);
final TextView clave = (TextView) findViewById(R.id.txtClave);
final Button ingresar = (Button) findViewById(R.id.btnIngresar);
ingresar.setOnClickListener(new View.OnClickListener() { //EVENTO BOTON INGRESAR
@Override
public void onClick(View view) {
trama = "" + fecha + "|" + "001" + "|" + dni.getText() + "|" + clave.getText() + "";
client.connect(getApplicationContext(), "181.170.192.241", 5000, trama);
}
});
}
Gracias!
Valora esta pregunta


0