Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/07/2017, 13:04
franjgg
 
Fecha de Ingreso: marzo-2007
Mensajes: 751
Antigüedad: 17 años, 2 meses
Puntos: 4
Envia los campos vacios

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 original
  1. protected void onCreate(@Nullable Bundle savedInstanceState) {
  2.         super.onCreate(savedInstanceState);
  3.  
  4.         setContentView(R.layout.activity_main);
  5.  
  6.         textView_lon = (EditText) findViewById(R.id.textView_lon);
  7.         textView_lat = (EditText) findViewById(R.id.textView_lat);
  8.  
  9.         //longitud_envio =(EditText) findViewById(R.id.longitud_envio);
  10.         //latitud_envio =(EditText) findViewById(R.id.latitud_enivio);
  11.  
  12.  
  13.         b = (Button) findViewById(R.id.b);
  14.  
  15.         locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  16.  
  17.  
  18.         listener = new LocationListener() {
  19.             @Override
  20.             public void onLocationChanged(Location location) {
  21.                 //t.append("\n " + location.getLongitude() + " " + location.getLatitude());
  22.                 //textView_lon.setText("");
  23.                 //textView_lat.setText("");
  24.                 textView_lon.append("\n" + location.getLongitude());
  25.                 textView_lat.append("\n" + location.getLatitude());
  26.  
  27.                 //longitud_envio.append("\n" + location.getLongitude());
  28.                 //latitud_envio.append("\n" + location.getLongitude());
  29.  
  30.  
  31.             }
  32.  
  33.             @Override
  34.             public void onStatusChanged(String s, int i, Bundle bundle) {
  35.  
  36.             }
  37.  
  38.             @Override
  39.             public void onProviderEnabled(String s) {
  40.  
  41.             }
  42.  
  43.             @Override
  44.             public void onProviderDisabled(String s) {
  45.  
  46.                 Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  47.                 startActivity(i);
  48.             }
  49.         };
  50.  
  51.  
  52.        /* b = (Button) findViewById(R.id.b);
  53.  
  54.         b.setOnClickListener(new View.OnClickListener() {
  55.             @Override
  56.  
  57.  
  58.         });*/
  59.  
  60.  
  61.         b.setOnClickListener(new View.OnClickListener() {
  62.  
  63.             @Override
  64.             public void onClick(View v) {
  65.                 //noinspection MissingPermission
  66.  
  67.                 locationManager.requestLocationUpdates("gps", 500000000, 0, listener);
  68.  
  69.                 Thread tr=new Thread(){
  70.                     @Override
  71.                     public void run() {
  72.  
  73.                         final String res=enviarPost(textView_lat.getText().toString(),textView_lon.getText().toString());
  74.                         runOnUiThread(new Runnable() {
  75.                             @Override
  76.                             public void run() {
  77.                                 int r=objJON(res);
  78.                                 if(r>0){
  79.  
  80.                                     Intent i=new Intent(getApplicationContext(), Principal.class);
  81.                                     i.putExtra("latitud", textView_lat.getText().toString());
  82.                                     i.putExtra("longitud", textView_lon.getText().toString());
  83.                                     startActivity(i);
  84.  
  85.  
  86.                                 }else {
  87.                                     Toast.makeText(getApplicationContext(), "Coordenadas incorrectas",Toast.LENGTH_SHORT).show();
  88.                                 }
  89.                             }
  90.                         });
  91.                     }
  92.                 };
  93.  
  94.                 tr.start();
  95.  
  96.             }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.         });
  103.  
  104.     }
  105.  
  106.             public  String enviarPost(String textView_lon, String textView_lat){
  107.  
  108.  
  109.                 String parametros="longitud="+textView_lon+"&latitud="+textView_lat;
  110.                 HttpURLConnection conection=null;
  111.                 String respuesta="";
  112.  
  113.                 try{
  114.  
  115.                     URL url=new URL("http://www.cofranet.org/catastro/coordenadas.php");
  116.                     conection=(HttpURLConnection)url.openConnection();
  117.                     conection.setRequestMethod("POST");
  118.                     conection.setRequestProperty("Content-length", ""+Integer.toString(parametros.getBytes().length));
  119.  
  120.                     conection.setDoOutput(true);
  121.                     DataOutputStream wr=new DataOutputStream(conection.getOutputStream());
  122.                     wr.writeBytes(parametros);
  123.                     wr.close();
  124.  
  125.                     Scanner inStream=new Scanner(conection.getInputStream());
  126.  
  127.                     while(inStream.hasNextLine())
  128.                         respuesta+=(inStream.nextLine());
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                 }catch (Exception e){}
  135.  
  136.                 return respuesta.toString();
  137.  
  138.             }
  139.  
  140.  
  141.                 public int objJON(String rspta){
  142.  
  143.                     int res=0;
  144.                     try{
  145.                         JSONArray json=new JSONArray(rspta);
  146.                             if(json.length()>0)
  147.                                 res=1;
  148.  
  149.                     }catch (Exception e){}
  150.                     return res;
  151.                 }
  152.  
  153.  
  154.                 @Override
  155.                 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  156.                     switch (requestCode){
  157.                         case 10:
  158.                                 configure_button();
  159.  
  160.  
  161.                             break;
  162.                         default:
  163.                             break;
  164.                     }
  165.                 }
  166.  
  167.     void configure_button(){
  168.         // first check for permissions
  169.  
  170.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  171.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  172.                 requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
  173.                         ,10);
  174.  
  175.             }
  176.             return;
  177.         }
  178.         // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
  179.  
  180.     }

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.