conexion android-php-android

  • Respuestas:0
ramon del amo
  • Posts del Foro: 1

16 jul. 2017 11:26:37 vía Web

Buenos dias a todos.
Acabo de ingresar en vuestro foro porque estoy , con mi aplicacion android , en un callejon sin salida.
Mi problema es enviar unas variables a un servidor PHP y ,este, devolver las mismas a todos los clientes con la app abierta. La variable a enviar y recibir desde android es de tipo DOUBLE (son coordenadas).
Mi codigo en android es:

public class Tarea extends AsyncTask {
@Override
protected Integer doInBackground(String... params) {

            String lat = latitud.getText().toString();
            String lon = longitud.getText().toString();
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("hxxp//xxx.xxx.x.xx/servidor/index.php");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("lat", lat);
                nameValuePairs.add(new BasicNameValuePair("lon", lon);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                if (nameValuePairs.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "arraylist no lleva datos", Toast.LENGTH_SHORT).show();
                }
                HttpResponse response = httpClient.execute(httpPost);

                BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine())!=null){
                    sb.append(line+NL);
                }
                in.close();
                String respuesta = sb.toString();
                capturar2.setText(respuesta);
                publishProgress(Integer.valueOf(respuesta));



            } catch (Exception e) {
            }
        return null;
    }

    @Override
    protected void onPostExecute(Integer integer) {

    }
}

Y el index PHP es de lo mas basico:
ini_set('error_reporting',0);

$lat=$_POST['lat'];
$lon=$_POST['lon'];

echo $lat."\n".$lon;
    ?>

Comentarles tambien, que el servidor esta creado con WAMP, y que, supuestamente, funciona con normalidad.
A la hora de ejecutar la clase "Tarea" deberian aparecer en el Texview "capturar2" las coordenadas, y no lo he conseguido.
Cualquier ayuda seria agradecida.

Contestar