簡體   English   中英

從GWT客戶端點擊非GWT Servlet

[英]Hitting non-GWT servlet from GWT client-side

假設有一個第三方RESTful Web服務在以下位置公開了GET端點:

http://someservice.com/api/askAnyQuestion

我想點擊該服務,將我的問題放在查詢字符串上:

http://someservice.com/api/askAnyQuestion&q=Does%20my%20dog%20know%20about%20math%3F

如何從客戶端GWT應用程序訪問此服務? 我一直在閱讀RequestFactory教程,但是RF似乎僅用於提供數據訪問層(DAL)和CRUDding實體,而且我不確定是否適用於此用例。

如果有人可以提供代碼示例,而不僅僅是我已經閱讀過的GWT教程的鏈接,或者我也可能閱讀過---)的一些Google博客,則可以獲得額外的超級加分。

您可以使用RequestBuilder 成功地將其用於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
        }

請同時查看此問題以獲取跨站點請求的相關信息。

幾天前我遇到了同樣的問題,並嘗試使用requestBuilder實現它。 您將收到跨域腳本問題。

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

我確實通過對我的服務器的RPC請求來處理此問題,然后從那里對跨域URL進行了服務器端HTTP請求。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM