Still confuse parse JSON in GWT
- by graybow
Please help meee.
I create a project named 'tesdb3' in eclipse. I create the PHP side to access the database, and made the output as JSON.. I create the userdata.php in folder war. then I compile tesdb3 project. Folder tesdb3 and the userdata.php in war moved in local server(I use WAMP). I put the PHP in folder tesdb3. This is the result from my localhost/phpmyadmin/tesdb3/userdata.php
[{"kode":"002","nama":"bambang gentolet"},{"kode":"012","nama":"Algiz"}]
From that result I think the PHP side was working good.Then I create UserData.java as JSNI overlay like this:
package com.tesdb3.client;
import com.google.gwt.core.client.JavaScriptObject;
class UserData extends JavaScriptObject{
      protected UserData() {}   
      public final native String getKode() /*-{ return this.kode; }-*/;
      public final native String getNama()  /*-{ return this.nama;  }-*/;
      public final String getFullData() {
          return getKode() + ":" + getNama();
      }
}
Then Finally in the tesdb3.java:
public class Tesdb3 implements EntryPoint { 
    String url= "http://localhost/phpmyadmin/tesdb3/datauser.php";
    private native JsArray<UserData> getuserdata(String json)
    /*-{ 
        return eval(json); 
    }-*/;
    public void LoadData() throws RequestException{
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
        builder.sendRequest(null, new RequestCallback(){
            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("error " + exception);
            }
            public void onResponseReceived(Request request,
                    Response response) {        
                Window.alert("betul" + response.getText());
                    //data(getuserdata(response.getText()));
            }
        });
    }
public void data(JsArray<UserData> data){
    for (int i = 0; i < data.length(); i++) {
        String lkode =data.get(i).getKode(); 
        String lname =data.get(i).getNama();                
        Label l = new Label(lkode+" "+lname);
        tb.setWidget(i, 0, l);
      }
    RootPanel.get().add(new HTML("my data"));
    RootPanel.get().add(tb);
} 
    public void onModuleLoad() {        
        try {
            LoadData();
        } catch (RequestException e) {
        }
    }
}
The result just showing string  "my data". And the Window.alert(response.getText()) showing nothing. Whyy?