Search code examples
multithreadingdialogcodenameoneevent-dispatch-thread

Dialog wait for download and thread issuse


I'm trying to display a dialog when user attempts to download something, but when the dialog is dismissed, the app redirects back to an earlier form page. The download works but the next form does not; just keeps displaying a 'Loading' dialog and returning to the previous form.

The looping form was commented out of the code below..

    ConnectionRequest request = new ConnectionRequest();

//        InfiniteProgress ip = new InfiniteProgress();
//        Dialog dlg = ip.showInifiniteBlocking();
//        request.setDisposeOnCompletion(dlg);
          Hashtable param_data_url = new Hashtable();
          param_data_url.put("index", 0);
          param_data_url.put("rango", 10);
          param_data_url.put("ciudad_id", Constants.CodigoCiudadActual);

          String urlBusqueda = Constants.URL_SERVICIO_SHOW_PRODUCTOS_ADOM_MOVIL;
          String url_conexion = Constants.getUrlServicio(Constants.URL_SERVIDOR_APLICACION, Constants.URL_APLICACION, urlBusqueda, param_data_url);

          request.setUrl(url_conexion);

                    request.addResponseListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
NetworkEvent netEvt = (NetworkEvent) evt;
                            byte[] dataArray = (byte[]) netEvt.getMetaData();

                            try {
                                JSONParser p = new JSONParser();
                                InputStream is = new ByteArrayInputStream(dataArray);
                                Hashtable h = p.parse(new InputStreamReader(is));

                                Enumeration enumCate = h.elements();

                                productoDelegado proDel = new productoDelegado();

                                imagenDelegado imaDel = new imagenDelegado();

                                while (enumCate.hasMoreElements()) {
                                    Hashtable aux = (Hashtable) enumCate.nextElement();
                                    Enumeration enumCate2 = aux.elements();
                                    while (enumCate2.hasMoreElements()) {
                                        Hashtable aux2 = (Hashtable) enumCate2.nextElement();
                                        Enumeration enumCate3 = aux2.elements();
                                        String[] param = new String[8];
                                        param[0] = ((Hashtable) aux2.get("DomPro")).get("producto_id").toString();
                                        param[1] = ((Hashtable) aux2.get("Producto")).get("nombre").toString();
                                        param[2] = ((Hashtable) aux2.get("Producto")).get("precio").toString();
                                        param[3] = ((Hashtable) aux2.get("Producto")).get("tienda_id").toString();
                                        param[4] = ((Hashtable) aux2.get("Producto")).get("calificacion").toString();
                                        param[5] = ((Hashtable) aux2.get("Imagen")).get("id").toString();
                                        param[6] = ((Hashtable) aux2.get("Producto")).get("tipo").toString();
                                        param[7] = "1";

                                        try {
                                            Imagen ima = new Imagen();
                                            ima.setId(param[5]);
                                            Producto prod = new Producto(param[0], param[1], "", param[2], "", param[4], param[3], ima, "", 0, 0, null, param[6], param[7]);

                                            if (proDel.existProducto(Integer.valueOf(param[0]))) {
                                                proDel.createProducto(prod);
                                            }

                                        } catch (Exception ex) {

                                            System.out.println("Error insertanto producto");

                                        }

                                        String[] paramIma = new String[3];
                                        paramIma[0] = ((Hashtable) aux2.get("Imagen")).get("id").toString();
                                        System.out.println(paramIma[0]);
                                        paramIma[1] = ((Hashtable) aux2.get("Imagen")).get("url").toString();
                                        System.out.println(paramIma[1]);

                                        if(!((Hashtable) aux2.get("Imagen")).get("name").toString().equalsIgnoreCase("default.jpg")){
                                            Vector nombre = split(((Hashtable) aux2.get("Imagen")).get("name").toString(), ".");   
                                        paramIma[2] = nombre.elementAt(0).toString() + ".thumb." + nombre.elementAt(1).toString();
                                        }else{
                                        paramIma[2]=((Hashtable) aux2.get("Imagen")).get("name").toString();

                                        }

                                        Imagen ima = new Imagen(paramIma[0], paramIma[1], paramIma[2]);

                                        try {
                                            if (imaDel.existImagen(Integer.valueOf(ima.getId()))) {
                                                imaDel.createImagen(ima);
                                            }
                                        } catch (Exception ex) {
                                            System.out.println("Error insertanto producto");
                                        }
                                    }
                                }
                                //empieza a renderizar la lista

                                Vector vect = proDel.listProdDesta();
createContainer(getResourceFilePath(), "AvatarContProdDestacado");
                                Enumeration enumPro = vect.elements();
                                Vector VectorDatos = new Vector();
                                int i = 0;

                                while (enumPro.hasMoreElements()) {
                                    Producto pro = (Producto) enumPro.nextElement();
                                    Imagen imagenPro = imaDel.getImagen(Integer.valueOf(pro.getIcono_producto().getId()));
                                    Hashtable dato = new Hashtable();
                                    System.out.println(pro.getId());
                                    dato.put("UrlImagen", Constants.URL_SERVIDOR_APLICACION + Constants.URL_APLICACION + imagenPro.getUrl() + imagenPro.getName());
                                    dato.put("Producto_id", pro.getId());
                                    dato.put("Label2", pro.getNombre());
                                    dato.put("Label", pro.getPrecio());
                                    cmp2.addItem(dato);

                                                                          " timeout:"+is.getTimeout()
ImageDownloadService(dato.get("UrlImagen").toString(), cmp2, i, "avatar");
cmp2, i, "avatar", dato.get("Producto_id").toString(), new Dimension(100, 100), ImageDownloadService.PRIORITY_CRITICAL);

//            NetworkManager.getInstance().addToQueue(ids);
                                        System.err.println(dato.get("UrlImagen"));
                                        ImageDownloadService ids = new ImageDownloadService(dato.get("UrlImagen").toString(), cmp2, i, "avatar");
//                                        ids.setPriority(ImageDownloadService.PRIORITY_CRITICAL);

                                        NetworkManager.getInstance().addToQueue(ids);
                                    } catch (Exception ex) {

           System.out.println("Error en la descarga de imagen ImageDownloadService.createImageToStorage");

                                    }
                                    i++;
                                }

                              cmp2.repaint();
                            } catch (Exception ex) {
                                System.err.println("error");
                            }
                        }
                    });
                    NetworkManager.getInstance().addToQueue(request);
                }
            });

        }
return true;
}

Solution

  • Your indentation and block of code makes this almost impossible to read and you cut off the top of the call so its impossible to really know. However, since you return from some sort of method and return a value I'm assuming your logic incorrectly expects synchronous behavior where addToQueue is asynchronous.

    Try changing addToQueue into addToQueueAndWait.