My android app does not receive data

0

I am trying to send 2 coordinates (latitude and longitude) to a php server in netbeans. When executing the thread, the only thing I get is that the textview where the same coordinates sent by the server should be reflected, remains blank.

This is the thread that is executed when you press a button to capture the coordinates and send them to the server:

 public class Tarea extends AsyncTask<String,Integer,Integer> {

@Override
    protected void onPreExecute() {
        dialogo = new ProgressDialog(MainActivity.this);
        dialogo.setMessage("Enviando coordenadas......");
        dialogo.setProgressStyle(dialogo.STYLE_SPINNER);
        dialogo.setProgress(0);
        dialogo.setMax(100);
        dialogo.show();
    }

    @Override
    protected Integer doInBackground(String... params) {


            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://xxx.xxx.x.xx/servidor/index2.php");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("lat", latitud.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("lon", longitud.getText().toString() ));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                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();
            } catch (Exception e) {
            }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        dialogo.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Integer integer) {
        dialogo.dismiss();
    }
}

And this is the PHP code in netbeans with Wamp server:

<?php
    ini_set('error_reporting',0);

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

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

?>
    
asked by boito 20.07.2017 в 20:25
source

1 answer

-1

In the class AsyncTask you can not use doInBackground to do anything with the user interface this runs on a separate thread to the interface. To work with the ui it must be in the methods onProgressUpdate,onPostExecute,onPreExecute

To check if you recive data use Log Ex: Log.d("Datos:",respuesta); and see if it works in the Android Studio console.

To change the edit and make it work move capturar2.setText(respuesta); in onPostExecute

It is changed like this: (the last parameter is String, which is the type of the result)

 public class Tarea extends AsyncTask<String,Integer,String> {


        @Override
        protected String doInBackground(String... strings) {
           ....
            return respuesta;
        }

        @Override
        protected void onPostExecute(String respuesta) {
            ...
            Log.d("Datos:",respuesta);//Si quieres verlo en el log
            capturar2.setText(respuesta);
        }
    }
    
answered by 20.07.2017 в 22:05