PDA

View Full Version : saving xml to a String variable



Tomek
17 Oct 2009, 1:54 AM
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.



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?

sven
17 Oct 2009, 2:04 AM
The request is async. Make sure you only access "temp" after onResponseReceived was called.

Tomek
18 Oct 2009, 1:52 AM
Could I do this that someone would be able to use my class like that?:

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

mariusz.pala
18 Oct 2009, 10:21 AM
Sven has already replied on your question, the request is asynchronous and you can access the result only after the callback function was called.

Tomek
19 Oct 2009, 12:26 AM
ok, thanks

micgala
19 Oct 2009, 4:10 AM
It seems hard at first, but with time you get use to work asynchronously.
Events is something you should work with.

Regards,
Michel.