Open a URL in the background

1

I have a method to dynamically create objects of type MenuItem based on a list of operations that I have created. Within that method I have the following FOR loop:

     for(OperacionConfigurableDTO oc : this.plantillaPrincipalFichaPacienteVM.getListaOperacionesConfigurables()){
            if(comprobarEstado(oc) && comprobarPerfil(oc) && comprobarActiva(oc)){
                Menuitem m = new Menuitem();

                AImage imagen = null;
                try {
                    imagen = new AImage("", oc.getIcono());
                    m.setImageContent(imagen);
                } catch (IOException e) {

                    e.printStackTrace();
                }

                m.setImageContent(imagen);
                m.setTooltiptext(oc.getNombre());
                m.setStyle("text-align: center;");
                m.setWidth("50px");

                m.addEventListener("onClick",new EventListener<MouseEvent>(){
                     @Override
                      public void onEvent(MouseEvent event) throws Exception {
                            System.out.println("PULSADO BOTON");

                     }
                });
                menuOpciones.appendChild(m);
                contadorOperaciones++;

            }   
    }

Inside that onClick that I am creating for each element (since ZK's MenuItem does not bring it), I need to execute a URL, but in the background, without opening a new tab in my browser (in case I want to make a call to a service). But after a long time of attempts I do not get it, I just call the URL opening a new tab.

    
asked by Angel Gonzalez Pena 13.11.2018 в 10:52
source

1 answer

1

I do not know the framework you mention but you can create a runnable to run it from another Thread and let you know when it's finished.

class BuscarUrl implements Runnable{
   BuscarUrl.Listener callbackListener;

   public BuscarUrl(Listener cbListener){
       this.callbackListener = cbListener;
   }

   @Override
   public void run(){
       try{
         URL url = new URL("http://www.example.com");
         HttpURLConnection con = (HttpURLConnection) url.openConnection();
         con .setRequestMethod("GET");
         con .setDoOutput(true);
         con.connect();
         InputStream is = con.getInputStream();

         // Aca leer la respuesta del input stream 

         callbackListener.finOk(); //Se podria agregar un parametro si querés pasar un resultado
       }catch(Exception e){ 
          e.printstacktrace();
          callbackListener.finConError(e);
       }
   }

   public static interface Listener {
        public void finOK();
        public void finConError(Exception e);
   }
}

Then:

m.addEventListener("onClick",new EventListener<MouseEvent>(){
                 @Override
                  public void onEvent(MouseEvent event) throws Exception {
                        System.out.println("PULSADO BOTON");
                        BuscarUrl.Listener listener = new BuscarUrl.Listener(){
                               // impelemntar los métodos de callback
                        }
                        new Thread(new BuscarUrl(listener)).start();
                 }
});

Note that the callback is executed in another thread and many frameworks have a specific thread to update the user interface. That is, you probably can not update the screen from the callback.

It is also common that these frameworks include a function to update the user interface from other thread (example runOnUiThread (), or similar names), if it were the case of the callback you would have to use this function.

The code I did not try so I may have an error.

    
answered by 13.11.2018 / 14:40
source