1. #1
    Ext User
    Join Date
    Oct 2009
    Posts
    14
    Vote Rating
    0
    Tomek is on a distinguished road

      0  

    Default saving xml to a String variable

    saving xml to a String variable


    Hello guys,
    I'm writing my first project in Ext GWT and I've got the following
    problem:

    I want to load XML file and save it to a local String variable.
    Code:
     
    public class XmlFile  { 
     
            String temp; 
     
            public void greetServer() { 
     
            RequestBuilder requestBuilder = new RequestBuilder 
     (RequestBuilder.GET,   "Test.gwt.xml"); 
     
            try { 
                     requestBuilder.sendRequest(null,new RequestCallback() { 
     
                            @Override 
                             public void onResponseReceived(Request request, Response response) 
     { 
     
                                    if (response.getStatusCode()==200) 
                                     { 
                                             temp=response.getText(); 
                                             System.out.println(temp); //HERE OK 
                                     } 
     
                            } 
     
                            @Override 
                             public void onError(Request request, Throwable exception) { 
                                     Window.alert(exception.getMessage().toString()); 
                             } 
     
                    }); 
             } catch (Exception ex) { 
                     Window.alert(ex.getMessage().toString()); 
             } 
     
    } 
     
    
    System.out.println(temp); //HERE ALWAYS NULL 
     }
    I have to save loaded file to "temp" variable because I have to do
    some other things with it in other classes. If I do it this way my
    "temp" varaible is always null outside "onResponseReceived" function.
    I've searched the Internet and found that it's because requestbuilder
    is asynchronous and I should "make callback of this function". How can
    I do that?

  2. #2
    Software Architect
    Join Date
    Sep 2007
    Posts
    13,971
    Vote Rating
    132
    sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light

      0  

    Default


    The request is async. Make sure you only access "temp" after onResponseReceived was called.

  3. #3
    Ext User
    Join Date
    Oct 2009
    Posts
    14
    Vote Rating
    0
    Tomek is on a distinguished road

      0  

    Default


    Could I do this that someone would be able to use my class like that?:

    XMLFIle file;
    file.greetServer();
    System.out.println(file.temp);

  4. #4
    Ext GWT Premium Member mariusz.pala's Avatar
    Join Date
    Jun 2008
    Location
    Poland
    Posts
    298
    Vote Rating
    2
    mariusz.pala is on a distinguished road

      0  

    Default


    Sven has already replied on your question, the request is asynchronous and you can access the result only after the callback function was called.

  5. #5
    Ext User
    Join Date
    Oct 2009
    Posts
    14
    Vote Rating
    0
    Tomek is on a distinguished road

      0  

    Default


    ok, thanks

  6. #6
    Sencha User
    Join Date
    Nov 2008
    Location
    Vienna - Austria
    Posts
    867
    Vote Rating
    1
    micgala is on a distinguished road

      0  

    Default


    It seems hard at first, but with time you get use to work asynchronously.
    Events is something you should work with.

    Regards,
    Michel.