Foros del Web » Programación para mayores de 30 ;) » Java »

Envia los campos vacios

Estas en el tema de Envia los campos vacios en el foro de Java en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 29/07/2017, 13:04
 
Fecha de Ingreso: marzo-2007
Mensajes: 751
Antigüedad: 17 años, 1 mes
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.
  #2 (permalink)  
Antiguo 30/07/2017, 01:11
Avatar de Profesor_Falken  
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: Envia los campos vacios

Buenas,

Seguramente te esta dando una excepcion, pero como la estas ignorando sistematicamente no puedes verla:

Código Java:
Ver original
  1. }catch (Exception e){}

Un saludo
__________________
If to err is human, then programmers are the most human of us
  #3 (permalink)  
Antiguo 01/08/2017, 00:29
Avatar de Fuzzylog  
Fecha de Ingreso: agosto-2008
Ubicación: En internet
Mensajes: 2.511
Antigüedad: 15 años, 8 meses
Puntos: 188
Respuesta: Envia los campos vacios

Tracea el código con log, mira en qué punto se están perdiendo los valores de las variables, y si no has entendido todavía el porqué nos lo vuelves a indicar aquí.
__________________
if (fuzzy && smooth) {
fuzzylog = "c00l";
return true;
}

Etiquetas: campos, envia, internet, vacios
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:52.