user1768830
user1768830

Reputation:

Hitting non-GWT servlet from GWT client-side

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 ;-).

Upvotes: 0

Views: 313

Answers (2)

Sam
Sam

Reputation: 2747

I had the same problem few days ago and tried to implement it with requestBuilder. You will receive a Cross-Domain Scripting issue.

https://developers.google.com/web-toolkit/doc/1.6/FAQ_Server#How_can_I_dynamically_fetch_JSON_feeds_from_other_web_domains?

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();
        }
    }
}

Upvotes: 0

udalmik
udalmik

Reputation: 7988

You can use RequestBuilder. Successfully used it to work with REST.

         RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
         try {
            builder.sendRequest(null, new RequestCallback() {
                @Override
                public void onError(Request request, Throwable exception) {
                    // process error
                }

                @Override
                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        // process success
                    } else {
                        // process other HTTP response codes
                    }
                }
            });
        } catch (RequestException e) {
            // process exception
        }

Please also take a look at this question for cross site requests related info.

Upvotes: 1

Related Questions