Let's say there is a 3rd party RESTful web service exposing a GET endpoint at:
http://someservice.com/api/askAnyQuestion
And I want to hit that service, placing my question on the query string:
http://someservice.com/api/askAnyQuestion&q=Does%20my%20dog%20know%20about%20math%3F
How do I hit this service from a client-side GWT application? I've been reading the RequestFactory
tutorials, but RF seems to be only for providing a data access layer (DAL) and for CRUDding entities, and I'm not entirely sure if it's appropriate for this use case.
Extra super bonus points if anyone can provide a code sample, and not just a link to the GWT tutorials, which I have already read, or some Googler's blog, which I have also probably read ;-).
I had the same problem few days ago and tried to implement it with requestBuilder. You will receive a Cross-Domain Scripting issue.
I did handle this by a RPC Request to my Server, and from there a Server-Side HTTP Request to the Cross-Domain URL.
https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite
public static void SendRequest(String method, String notifications) {
String url = SERVICE_BASE_URL + method;
JSONObject requestObject = new JSONObject();
JSONArray notificationsArray =null;
JSONObject mainRequest = new JSONObject();
try {
notificationsArray = new JSONArray(notifications);
requestObject.put("notifications", notificationsArray);
mainRequest.put("request", requestObject);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection connection = null;
try
{
URL server = new URL(url);
connection = (HttpURLConnection) server.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.writeBytes(mainRequest.toString());
writer.flush();
writer.close();
parseResponse(connection);
}
catch (Exception e)
{
System.out.println("An error occurred: " + e.getMessage());
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
}