Hi Gustavo,

I want to know the server side how you are adding data to JSONArray and
sending this JSONArray as response.
using Java GWT we are adding like this form.
1) we have to add JSONArray to ArrayList.
For Example:
 in server side resonse
I am adding
String name = "Ram";
ArrayList alList = new ArrayList();
aList.add(name);
JSONObject jsonDataToReturn = new JSONObject();
 JSONArray jsonResult = new JSONArray(alList);
 response.getWriter().print(jsonDataToReturn);

In Client side u have to create class like
public class JsName extends JavaScriptObject{

    protected JsName (){    }
    public final native int getName() /*-{ return this.name; }-*/;

}
}

in the client response
 you have to add like

JsArray<JsName > jsCatgAttr = asArrayOfName(strResponse);
String name = jsCatgAttr.getName();                            //this is
getting from JsName Object

private final native JsArray<JsName > asArrayOfName(String json) /*-{
        return eval(json);
      }-*/;


you try this way you definitely get better solution .

If you have any Idea on hanging problem on IE8 please provide me a solution.

Bey..

Thanks,

Raj.

On Sun, Jul 18, 2010 at 4:27 PM, Gustavo <[email protected]> wrote:

> 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]<google-web-toolkit%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
>

-- 
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.

Reply via email to