PDA

View Full Version : proxy question



hiinsu
25 Jun 2010, 9:18 AM
Hi Sven

I'm currently using script tag proxy to get data along with GWT jsonpReqest class because I dont really know how to handle errors in gxt's proxy method, and manipulating and playing with data after it's retrieved.

How can i do this?

sven
25 Jun 2010, 9:20 AM
I dont understand what you mean, please post a testcase that implements EntryPoint and shows what you have. Also post what you want it to be.

hiinsu
25 Jun 2010, 9:25 AM
This is basic structure of GWT JsonpRequst implemntation Im using
there is async callback that allows onError case, and onSuccess

is there similar functionality in gxt? specifically using script tag proxy



import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayoutData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import project.client.model.JSONData; // json data overlay

public class Sandbox extends EntryPoint {

private static LayoutContainer lc;

public void onModuleLoad() {
super.onRender(parent, index);
setLayout(new VBoxLayout());

initialize();

lc.add(new Label("make test request"));

test();
}

private void initialize() {
lc = this;
lc.setHeight("100%");
lc.setLayoutData(new VBoxLayoutData(new Margins(10)));
}

private void test() {
String url = GWT.getHostPageBaseURL() + "testdata/test.json";
JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.requestObject(url, new AsyncCallback<JSONData>() {
@Override
public void onFailure(Throwable caught) {
lc.add(new Label("test request failed"));
System.out.println("Error: " + caught);
try {
throw caught;
} catch (IncompatibleRemoteServiceException e) {
// this client is not compatible with the server;
// cleanup and refresh the browser
} catch (InvocationException e) {
// the call didn't complete cleanly
} catch (Throwable e) {
// last resort -- a very unexpected exception
}
}

@Override
public void onSuccess(JSONData data) {
lc.add(new Label("test request made"));
show(data);
}
});
}

public static void show(JSONData data) {
lc.add(new Label("parsing data"));
int id = data.getId();
String method = data.getMethod();
JSONObject result = data.getResult();
JSONObject error = data.getError();

lc.add(new Label("data parsed"));
Label lb1 = new Label("ID : " + id);
Label lb2 = new Label("Method " + method);
Label lb3 = new Label("Result :" + result.toString());
Label lb4 = new Label("error : " + error);

lc.add(new Label("print data"));
lc.add(lb1);
lc.add(lb2);
lc.add(lb3);
lc.add(lb4);
lc.add(new Label("print data done"));

lc.layout();
lc.syncSize();
}
}

sven
25 Jun 2010, 9:26 AM
There is, if you use it together with a Loader, than the loader fires a Load and a LoadException event

hiinsu
25 Jun 2010, 9:28 AM
you mean something like this?



public class Jsonp {

@SuppressWarnings("unchecked")
public static void asyncCall(
final String url, // URL to retrieve JSON result
ModelType type, // ModelType to define JSON format
final EventType successEvent, // Event needs to be fired after a successful communication
final EventType failureEvent, // Event needs to be fired after a failed communication
int timeout) { // Timeout in seconds

ScriptTagProxy proxy = new ScriptTagProxy(url);
JsonPagingLoadResultReader reader = new JsonPagingLoadResultReader(type);
final PagingLoader loader = new BasePagingLoader(proxy, reader);
final ListStore store = new ListStore(loader);

final Timer t = new Timer() {
public void run() {
loader.removeAllListeners();
Dispatcher.forwardEvent(failureEvent, "ERROR: timeout");
}
};

loader.addListener(Loader.Load, new Listener() {
@Override
public void handleEvent(BaseEvent be) {
t.cancel();
int count = store.getCount();
if (count == 0) {
Dispatcher.forwardEvent(failureEvent, "Zero result(s)");
} else
Dispatcher.forwardEvent(successEvent, store);
}
});

loader.addListener(Loader.LoadException, new Listener() {
@Override
public void handleEvent(BaseEvent be) {
Dispatcher.forwardEvent(failureEvent, "Exception: " + ((LoadEvent)be).exception.toString());
}
});
loader.load();
t.schedule(timeout*1000);
}
}

hiinsu
25 Jun 2010, 9:32 AM
initially, I used general event update, load, update error, update success, etc...
but then I had hard time passing out the event to class that made request.

so adding the event type made it passing the event data easier. but it seemed to require lots of events..
pretty much 2~4 events for form, or classes that make request

any suggestion?