Hello everyone, I have a little problem. My code sends a request to
the server and onResponseReceived a certain ArrayList<String> will
receive elements from a JSONArray. After the ArrayList<string> gets
its elements and after the request is complete I want to read the
ArrayList<String>, but it is now empty and I don't know why.
Here I reproduce the same situation:
package requestTest.client;
import java.util.ArrayList;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class RequestTeste implements EntryPoint {
/**
* The message displayed to the user when the server cannot be
reached or
* returns an error.
*/
/**
* This is the entry point method.
*/
public void onModuleLoad() {
final ArrayList<String> list = new ArrayList<String>();
final ArrayList<Integer> trick = new ArrayList<Integer>();
String url = "requestTeste.php";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
URL.encode(url));
builder.setHeader("Content-type", "text/plain");
builder.setCallback(new RequestCallback(){
public void onResponseReceived(Request request,
Response response)
{
if (200 == response.getStatusCode()) {
// parse the response text into JSON
JSONValue jsonObject =
JSONParser.parse(response.getText());
JSONArray jsonArray =
(JSONArray)jsonObject;
if (jsonArray != null) {
for (int
i=0;i<jsonArray.size();i++){
list.add(jsonArray.get(i).toString());
}
RootPanel.get().add(new Label("Here is the first try"));
RootPanel.get().add(new
Label(list.toString()));
RootPanel.get().add(new
Label(Integer.toString(trick.get(0))));
} else {
//TODO
}
} else {
Window.alert("Couldn't retrieve JSON
(" +
response.getStatusText() + ")");
}
}
public void onError(Request request, Throwable
exception) {
// TODO Auto-generated method stub
}
});
try {
builder.send();
} catch (RequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RootPanel.get().add(new Label("Here is the second
try"));
RootPanel.get().add(new Label(list.toString()));
}
}
}
}
and here is the php code running on the server
<?php
$var = array("a1","a2","a3");
echo json_encode($var);
?>
As you may see, the second try comes out before the first try, which
is my problem.
Thank you for your help,
Gustavo
Brazil
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.