Write String in a network file

1

I would like to make a kind of form with this code by sending "string_ship" but I can not get it to write it in the file.

    public void envio_formulario(View v){
        String nombre=et_nonm.getText().toString();
        String telefono=et_telf.getText().toString();
        String email=et_email.getText().toString();
        String comentario=et_coment.getText().toString();

        cadena_envio="Nombre: "+nombre+" Email: "+email+" Telefono: "+telefono+" Comentario: "+comentario+"\n";
        Toast.makeText(this,cadena_envio,Toast.LENGTH_LONG).show();
        Thread hilo_escribe = new Thread(new Runnable() {
            public void run() {
                try{
                    URL url = new URL("http://radios-android.000webhostapp.com/formulario.txt");
                    URLConnection urlConnection = url.openConnection();
                    OutputStream is = urlConnection.getOutputStream();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(is));
                    bw.write(cadena_envio);
                    bw.close();
                }catch (Exception e){}
            }
        });
        hilo_escribe.start();

        try{
            hilo_escribe.join();
        }catch (Exception e){}


    }
}
    
asked by Mario AzcVei 08.05.2018 в 22:13
source

1 answer

0

First of all this type of operations should not be done in the main thread, you can use a Asynctask to call your method, example:

 private class LeeArchivos extends AsyncTask<Void , Integer, Long>{
        @Override
        protected Long doInBackground(Void... voids) {

            return procesoEscritura;
        }

        @Override
        protected void onPostExecute(Long result) {
          //Termina proceso
            Log.i("TAG" , "Termina proceso de escritura.");
        }
    }

Now, if the goal is to write to a file you can do it using FTP, for this you can use SimpleFTP

Otherwise you can send what you want to write to the file via POST and that a page PHP, ASP, JSP, etc write to the file, as indicated by this response on the site in English:

link

    
answered by 09.05.2018 в 00:12