Ver Mensaje Individual
  #2 (permalink)  
Antiguo 31/03/2013, 15:20
SMai
 
Fecha de Ingreso: marzo-2013
Mensajes: 5
Antigüedad: 11 años, 1 mes
Puntos: 0
Información Respuesta: Error in http connection android.os.NetworkOnMa

Buenas amig@s!!! Mientras esperaba si alguien me iluminaba con el camino, leyendo y releyendo post, blog etc. he "solucionado" el tema temporalmente.

Os copio mi clase Post la cual he tenido que extenderla de AsyncTask para poder ejecutarla en un hilo distinto.
Para poder obtener el resultado en la clase comunicación, la cual era la que llamaba a esta, he realizado una chapucilla con unas variables publicas estáticas (datos y procesoTerminado) ya que no podia parar esta (no se como) hasta obtener el resultado de la ejecución de la clase Post.

Código:
public class Post extends AsyncTask<ArrayList<String>, Void, Void>{
	private InputStream is = null;
	private String respuesta = "";
	@Override
	protected Void doInBackground(ArrayList<String> ... parametros) {
		String aux = null;
		conectaPost(parametros[0], "http://URL/archivo.php");
		if (is != null) {
			getRespuestaPost();
			aux = respuesta;
		}
		Comunicaciones.datos = aux; //CHAPUZA TEMPORAL
		Comunicaciones.procesoTerminado = true; //CHAPUZA TEMPORAL
		return null;
	}	
	
	private void conectaPost(ArrayList<String> parametros, String URL) {
		ArrayList<BasicNameValuePair> nameValuePairs;
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(URL);
			nameValuePairs = new ArrayList<BasicNameValuePair>();
			if (parametros != null) {
				for (int i = 0; i < parametros.size() - 1; i += 2) {
					nameValuePairs.add(new BasicNameValuePair(
							(String) parametros.get(i), (String) parametros
									.get(i + 1)));
				}
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			}
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error en http connection " + e.toString());
		}
	}

	private void getRespuestaPost() {
		try {
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();

			respuesta = sb.toString();
			Log.e("log_tag", "Cadena recibida --> " + respuesta);
		} 
		catch (Exception e) {
			Log.e("log_tag", "Error al convertir resultado " + e.toString());
		}
	}
Se aceptan correcciones y criticas!