Android - Funcion con nuevo hilo

 
Vista:
Imágen de perfil de Efrain

Funcion con nuevo hilo

Publicado por Efrain (2 intervenciones) el 15/12/2015 01:13:46
Hola

Tengo una funcion y dentro un nuevo hilo ...funciona bien ...pero para que setee el editex debo ejecutar la funcion 2 veces la primera devuelve vacio

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
@Override
public void onClick(View v)
{
 
	editText1.setText(TestConexionGet());
}
 
private String   TestConexionPost () {
 
 
	Thread  nt = new Thread(new Runnable() {
		@TargetApi(Build.VERSION_CODES.KITKAT)
		@Override
		public void run(){
			Looper.prepare();
			HttpURLConnection con = null;
			BufferedReader responseReader = null;
			try {
				// Construir los datos a enviar
				data = "POST=" + URLEncoder.encode("POST","UTF-8");
				url= new URL(MiUrl2);
				con = (HttpURLConnection)url.openConnection();
				// Activar método POST
				con.setDoOutput(true);   con.setDoInput(true);
				// Tamaño previamente conocido
				con.setFixedLengthStreamingMode(data.getBytes().length);
 
				con.setInstanceFollowRedirects(false);
				con.setRequestMethod("POST");
 
				// Establecer application/x-www-form-urlencoded debido a la simplicidad de los datos
				con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 
				con.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length));
				con.setUseCaches(false);
 
				OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
				writer.write(data);
				writer.flush();
 
				String line;
				BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
				data = "" ;
				while ((line = reader.readLine()) != null) {
					data = data + line ;
				}
				writer.close();
				reader.close();
				String part[] = String.valueOf(data).split("#");
				data = part[0].replace("\"", "");
				makeText(getApplicationContext(), String.valueOf(data) + " 1.1", Toast.LENGTH_LONG).show();
				Looper.loop();
			} catch (IOException e) {
				makeText(getApplicationContext(), String.valueOf(e) + " 1.2", Toast.LENGTH_LONG).show();
				Looper.loop();
				con.disconnect();
			} finally {
				if(con!=null)
					con.disconnect();
			}


		}
	});
	nt.start();
	return data ;// La variable data es un atributo de la clase 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder