How to translate,use JSON in GWT?
Posted
by graybow
on Stack Overflow
See other posts from Stack Overflow
or by graybow
Published on 2010-04-21T06:21:46Z
Indexed on
2010/04/21
6:23 UTC
Read the original article
Hit count: 284
gwt
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.POST, 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) {
getuserdata(response.getText());
//this is how i use the userdata json(is this already translated?)
UserData UD = null;
String LKode =UD.getKode();
String LName =UD.getNama();
Label L = new Label(LKode+""+LName);
RootPanel.get().add(L);
}
});
}
public void onModuleLoad() {
try {
LoadData();
} catch (RequestException e) {
e.printStackTrace();
}
}
}
The result is blank(i use development mode). and there was an eror like this:(I show it just some part)
10:46:29.984 [ERROR] [tesdb3] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (ReferenceError): json is not defined
fileName: http://localhost:1092
lineNumber: 2
stack: ("")@http://localhost:1092:2
My question is:
How I use the translated Json in right way??
Is there any wrong use from my code?
Is that necessary to move the compiled project to local server folder?(i do it following a tutorial from google).
Sorry too many ask. but i'm really really confused.
© Stack Overflow or respective owner