Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: How to get data from onSuccess method of RPC call

  1. #11
    Sencha User
    Join Date
    Sep 2011
    Posts
    4

    Default

    Not yet! OMG !

    I send in attch my code.
    And after I call the method "construtor" in my principal class of GWT, I have null object then. =/
    I don't understand


    Code:
    public class PontoClass {
        private String [] aux = new String [12];
        protected String [] ids = new String [2];
        protected Teste retorno = new Teste();
        
        
        public PontoClass(){}
        
        public Teste construtor(final String id_local){
            
            
            final ConexaoAsync conexao = GWT.create(Conexao.class);
                    
            conexao.conectar(
                    new AsyncCallback<String>() {
                        public void onFailure(Throwable caught) {
                            Window.alert("Erro ao conectar ao banco!");
                        }
    
    
                        public void onSuccess(String result) {
                                                    
                        }
                    });
            
            
            
            conexao.select("SELECT idlocais,nome " +
                            "FROM monografia.locais" +
                            " WHERE idlocais = "+id_local, new AsyncCallback<ArrayList<String>>() {
                public void onFailure(Throwable caught) {
                    Window.alert("Erro no select! "+caught);
                }
    
    
                @Override
                public void onSuccess(ArrayList<String> result) {
                    
                    for(int i = 0; i < result.size(); i++){
                        //System.out.println(result.get(i).toString());
                        aux = result.get(i).toString().split("####");
                        ids[0] = aux[0];
                        ids[1] = aux[1];
                        montar_objeto();
                    }
                    
                    
                }
            });
            
            System.out.println("idddddddd= "+retorno.id); // ALWAYS IS NULL
            System.out.println("vetor id = "+ids[1]); // ALWAYS IS NULL
            return retorno;        
        }
        
        
        public void montar_objeto(){
            retorno.id = ids[0];
            retorno.nome = ids[1];
        }
    }
    
    
    class Teste{
        public String id;
        public String nome;
        public String descricao;
        public String tags;
        public String contato;
        public int tipo;
        public String geo;
        public int curtiu;
        public int nao_curtiu;
        public int ativo;
        public String data_in;
        public String data_out;
    }

  2. #12
    Sencha User
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,737

    Default

    Here is a way that might help. Without async, your code might look like this

    //start function A
    //make call to server, and have return value
    //deal with return value
    //end function A

    Making things async generally looks more like this:

    //start function A
    //make call to server, with callback to function B
    //end function A
    //start function B
    //deal with return value
    //end function B

  3. #13
    Sencha User
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,737

    Default

    As in my last message, try moving the code that needs the string response into the onSuccess method, or, as many people prefer, into a new method.

    In your example, instead of the sys.out.println calls at the end of montar_objeto(). I see you are trying to return the value afterward - that is difficult to factor out into another method. Instead, I think you need to look at how to solve your problem in another way, instead of asking the method to return a value, think more in terms of having told the server to call the _client_ when it is ready with that value. This can make some other problems more difficult to solve, but in the end it usually makes for more flexible code, and better browser performance.

    Did you get a chance to go through that link?

  4. #14
    Sencha User
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,737

    Default

    Here is something that may help you to understand why you are never getting the value you expect to be returned. Add a break point in montar_objeto or in the onSuccess method, and add a breakpoint at the end of the construtor method - you will find that you hit the end of construtor before onSuccess is called, because the server is still working on it. It is _not possible_ to wait until the browser has finished, at least in GWT - in other ajax frameworks, you can, but it will cause the entire browser to hang, indefinitely if something happens to the server. Not good, and GWT is written to prevent this from happening.

  5. #15
    Sencha User
    Join Date
    Sep 2011
    Posts
    4

    Default

    Now it's work !!
    In montar_objeto I get the "map" and addOverlay, but my idea was mount a object Test or PontoClass with the info i got in return of select, and in my main class I'll work and set in map these points!
    But I see add now in montar_object are more easy, I'll think about this.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •