|
#1
|
|||
|
|||
|
Hi!
I'm using: SmartGWT 2.2 (LGPL) + GWT 2.04 on Linux. I need to retrieve some data from my server like: user configurarions and others. I try to use the JSON and RestDataSource. Follow my code: Code:
package com.test.client;
import com.google.gwt.core.client.EntryPoint;
....
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Test implements EntryPoint {
/**
* The message displayed to the user when the server cannot be reached or
* returns an error.
*/
private static final String SERVER_ERROR = "An error occurred while "
+ "attempting to contact the server. Please check your network "
+ "connection and try again.";
public static String UserName = "Gary Oldman";
public final static String URL = "/h/j.js";
// Time in milliseconds (10 seconds)
public static double REQUEST_TIMEOUT = 10.0 * 1000.0;
/**
* This is the entry point method.
*/
public void onModuleLoad() {
// ===========================================================
// Create the Fake RestDataSource
// ===========================================================
final RestDataSource jDS = new RestDataSource()
{
/**
* Transform the request
*/
@Override
protected Object transformRequest(DSRequest dsRequest)
{
// Set some attributes
dsRequest.setContentType("application/json");
dsRequest.setUseSimpleHttp(true);
// Set a timeout in milliseconds
RPCManager.setDefaultTimeout(REQUEST_TIMEOUT);
// Set a prompt for this request
RPCManager.setTimeoutErrorMessage(SERVER_ERROR);
// Set some attributes
switch( dsRequest.getOperationType() )
{
case FETCH:
break;
case ADD:
break;
case UPDATE:
break;
case REMOVE:
break;
default:
break;
}
return super.transformRequest(dsRequest);
}
/**
* Transform the response into Java code.
*/
@Override
protected void transformResponse(DSResponse response, DSRequest request, Object jsonData) {
/**
* Response:
* {"response":{"status":0,"data":{"username":"John Doe","Id":"1","operatorid":"1"}}}
*/
// Get some data
JSONArray values = XMLTools.selectObjects(jsonData, "/response/data/username");
UserName = ((JSONString) values.get(0)).stringValue();
}
};
jDS.setID("JSONTestDS");
jDS.setFetchDataURL(URL);
jDS.setAddDataURL(URL);
jDS.setUpdateDataURL(URL);
jDS.setRemoveDataURL(URL);
jDS.setDataFormat(DSDataFormat.JSON);
jDS.setDataProtocol(DSProtocol.POSTPARAMS);
jDS.setClientOnly(false);
// Create the DataSource fields
DataSourceIntegerField idField = new DataSourceIntegerField("Id");
idField.setPrimaryKey(true);
jDS.setFields(idField);
// ===========================================================
// Create the Buttons
// ===========================================================
IButton fetch = new IButton("Fetch data");
fetch.addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
Criteria criteria = new Criteria("Id", "10");
jDS.fetchData(criteria);
SC.say("User name: "+UserName);
}
});
IButton show = new IButton("Show Console");
show.addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
SC.showConsole();
}
});
// ===========================================================
// Create the layouts
// ===========================================================
HLayout hlayout = new HLayout();
hlayout.setMembersMargin(5);
hlayout.addMember(fetch);
hlayout.addMember(show);
hlayout.draw();
}
}
Code:
{"response":{"status":0,"data":{"username":"John Doe","Id":"10","operatorid":"1"}}}
Code:
20:39:02.005:MUP3:INFO:RPCManager:sendQueue[0]: 1 RPCRequest(s); transport: xmlHttpRequest; target: /h/j.js
20:39:02.011:MUP3:DEBUG:RPCManager:XMLHttpRequest GET from /h/j.js with fields: {Id: "10",
_operationType: "fetch",
_dataSource: "JSONTestDS"} full URL string: /h/j.js?Id=10&_operationType=fetch&_dataSource=JSONTestDS
20:39:02.071:XRP7:INFO:RPCManager:transaction 0 arrived after 64ms
20:39:02.073:XRP7:DEBUG:RPCManager:Result string for transaction 0: "{"response":{"status":0,"data":{"username":"John Doe","Id":"10","operatorid":"1"}}}
"
20:39:02.075:XRP7:INFO:RPCManager:rpcResponse(unstructured) results -->"{"response":{"status":0,"data":{"username":"John Doe","Id":"10","operatorid":"1"}}}
"<--
The problems: 1) No error generated but the UserName don't change. 2) Even saying to use POST, never works. Only sends GETs. Not so important. Another question: How to get the current <i>locale</i> value? Any help? I'm missing something? Thanks in advance, Andre Derraik. |
|
#2
|
|||
|
|||
|
Hi.
I again. But I think found the problem for question (1): Synchronization. I need to wait the request arrive to use the data. I.e., when call the fetchData() then next command will be executed, even the fetch don terminated. Thats the answer? New code for the fetch button: Code:
IButton fetch = new IButton("Fetch data");
fetch.addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
Criteria criteria = new Criteria("Id", "10");
jDS.fetchData(criteria, new DSCallback() {
@Override
public void execute(DSResponse response, Object rawData, DSRequest request) {
SC.say("User name: "+UserName);
}
});
}
});
Can I suspend the program until the fetch finish? Thanks, Andre Derraik. |
|
#3
|
|||
|
|||
|
Anyone have some good ideas on a solution?
FetchData, Wait, Proceed with fetch results I'm using SmartGWT 2.3 |