Buenas que tal,
Vereis tengo este código que localiza las coordenadas y las envia a php, el tema es que la mayoria de las veces las envia vacias, y no veo el por que.
Código Java:
Ver originalprotected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_lon = (EditText) findViewById(R.id.textView_lon);
textView_lat = (EditText) findViewById(R.id.textView_lat);
//longitud_envio =(EditText) findViewById(R.id.longitud_envio);
//latitud_envio =(EditText) findViewById(R.id.latitud_enivio);
b
= (Button) findViewById
(R.
id.
b);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//t.append("\n " + location.getLongitude() + " " + location.getLatitude());
//textView_lon.setText("");
//textView_lat.setText("");
textView_lon.append("\n" + location.getLongitude());
textView_lat.append("\n" + location.getLatitude());
//longitud_envio.append("\n" + location.getLongitude());
//latitud_envio.append("\n" + location.getLongitude());
}
@Override
public void onStatusChanged
(String s,
int i, Bundle bundle
) {
}
@Override
public void onProviderEnabled
(String s
) {
}
@Override
public void onProviderDisabled
(String s
) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
};
/* b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
});*/
b.
setOnClickListener(new View.
OnClickListener() {
@Override
public void onClick
(View v
) { //noinspection MissingPermission
locationManager.requestLocationUpdates("gps", 500000000, 0, listener);
@Override
public void run() {
final String res
=enviarPost
(textView_lat.
getText().
toString(),textView_lon.
getText().
toString()); @Override
public void run() {
int r=objJON(res);
if(r>0){
Intent i
=new Intent
(getApplicationContext
(),
Principal.
class); i.putExtra("latitud", textView_lat.getText().toString());
i.putExtra("longitud", textView_lon.getText().toString());
startActivity(i);
}else {
Toast.makeText(getApplicationContext(), "Coordenadas incorrectas",Toast.LENGTH_SHORT).show();
}
}
});
}
};
tr.start();
}
});
}
String parametros
="longitud="+textView_lon
+"&latitud="+textView_lat
;
try{
URL url
=new URL("http://www.cofranet.org/catastro/coordenadas.php"); conection.setRequestMethod("POST");
conection.
setRequestProperty("Content-length",
""+Integer.
toString(parametros.
getBytes().
length));
conection.setDoOutput(true);
wr.writeBytes(parametros);
wr.close();
Scanner inStream=new Scanner(conection.getInputStream());
while(inStream.hasNextLine())
respuesta+=(inStream.nextLine());
return respuesta.toString();
}
public int objJON
(String rspta
){
int res=0;
try{
JSONArray json=new JSONArray(rspta);
if(json.length()>0)
res=1;
return res;
}
@Override
public void onRequestPermissionsResult
(int requestCode, @NonNull
String[] permissions, @NonNull
int[] grantResults
) { switch (requestCode){
case 10:
configure_button();
break;
default:
break;
}
}
void configure_button(){
// first check for permissions
if (ActivityCompat.
checkSelfPermission(this,
Manifest.
permission.
ACCESS_FINE_LOCATION) != PackageManager.
PERMISSION_GRANTED && ActivityCompat.
checkSelfPermission(this,
Manifest.
permission.
ACCESS_COARSE_LOCATION) != PackageManager.
PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
,10);
}
return;
}
// this code won't execute IF permissions are not allowed, because in the line above there is return statement.
}
He probado comprobar los nombres de los campos pero nada parece todo correcto, como digo algunas veces, muy pocas envia los datos y los guarda.
A ver si alguien ve algo.
Saludos.