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;
?>