tags:

views:

141

answers:

3

I'm new in gwt. and need to know how to use JSON in gwt so i try this simple data loader but i'm still confuse.
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) {        
            //1                                                         
                            //data(getuserdata(response.getText()));
                            //2
                  JsArray<UserData> uda = JsonUtils.unsafeEval(response.getText()) 
                              data(uda);    
            }
        });
    }

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) {
            e.printStackTrace();
        }
    }
}

The result from both method i use in the onResponseReceived is the same. Just showing string "my data".
but the method 2 create eror like this:
14:41:59.875 [ERROR] [tesdb3] Uncaught exception escaped com.google.gwt.core.client.JavaScriptException: (SyntaxError): syntax error

Did I miss use the 2nd method?
Why method 1 didn't have any eror but can't show the data?

+2  A: 

It looks like a typo in your code, which brings me to naming conventions: for variables and methods use camel case, starting with a lower case character. Thus UserData UD should be UserData ud.

In your getuserdata method (which should be getUserData) you use the parameter name Json with capital J and in the native code json with the lower j. This explains the error.

Regarding the getUserData method. There is a GWT method: JsonUtils.unsafeEval(json) which you should use.

Furthermore, the code in the onResponseReceived seems incomplete, it might not be of importance as it might be incorrectly be put in this example, but just to be complete, here is what it should look like:

JsArray<UserData> uda = JsonUtils.unsafeEval(response.getText());

for (int i = 0; i < uda.length(); i++) {
  UserData ud = uda.get(i);
  String lKode = ud.getKode(); 
  String lName = ud.getNama();                 
  Label l = new Label(lKode + " " +lName);

  RootPanel.get().add(l);
}
Hilbrand
I try your example an edit the question. thanx
+1  A: 

The problem is that your JSON has incorrect syntax, you are missing a comma after the first item of the table, it should be (whitespace added for readability):

[
    {
        "kode": "002",
        "nama": "bambang gentolet" 
    },
    {
        "kode": "012",
        "nama": "Algiz" 
    }
]

Since JSON is a part of JavaScript this might be the syntax error exception you are getting.

PS: I'd recommend using some PHP framework to create JSON for you (Zend Framework is my usual choice). Also, JSON validators like JSONLint are very useful for debugging JSON.

Igor Klimer
Thanks. I create new valid Json format. but the result is the same. Is there any inherits in the xml or something like that?
There's `<inherits name='com.google.gwt.json.JSON'/>` but I'm not sure you need it if you're using JavaScript Overlay Types. And `<inherits name='com.google.gwt.http.HTTP'/>` for HTTP stuff (GET, POST), but you probably have that one already. Can you check with Firebug (or a similar tool) if there's any exception thrown to the browser?
Igor Klimer
i try use firebug. there was no error or something like that caught in firebug. You have any example loading database? I have no clue what to do anymore.
I don't see anything else wrong with your code - you have to check the whole path that the JSON goes, from the server to the browser to GWT. Use Firebug, see the "Net" tab, check if the response is indeed valid, if it has valid response type, then run the GWT app in a debugging session and check all the crucial stuff - the `response` object you get, etc.
Igor Klimer
A: 

I have the same problem...any solution yet?

DaRk kNigHT