2010-12-13 2 views
0

json-rsがjson形式で提供するリソースをリクエストできるプロジェクトがあります。 jsonが表示された残りのURLを照会すると、すべてがブラウザで正しく機能します。GWTとREST(jax-rs)

私は自分のGWTプロジェクトにこれらのリソースをリクエストして処理し、それらを私のインターフェースに表示させたいと思います。私が見つけた最も簡単な方法は、リクエストビルダーとオーバーレイを使用することです。コードはより低いです。問題は、コードが実行されているときに実際のRequestCallback()に入ることはないようです。ステータス文字列は変更されません。私はそれがSOPになるかもしれないと思ったので、私はまだそれを追加しました。どんな理想?

package com.workoutcell.client; 
//import com.google.gwt.core.client.JavaScriptObject; 
import com.google.gwt.core.client.JsArray; 
import com.google.gwt.http.client.*; 
import com.google.gwt.http.client.Request; 
import com.google.gwt.http.client.RequestBuilder; 
import com.google.gwt.http.client.RequestCallback; 
import com.google.gwt.http.client.RequestException; 
import com.google.gwt.http.client.Response; 

/** 
* 
* @author 
*/ 

public class RestToInfoSession{ 

    String queryReturn = null; 
    JsArray<InfoJSO> arrayOfInfo = null; 
    String host = "http://localhost:8080/mysite"; 
    String restModule = "/calendar/getinfo"; 
    String id = null; 
    String year = null; 
    String month = null; 
    String status = "Not Initialized"; 

    public RestToInfoSession(String id, String year, String month){ 

     this.id =id; 
     this.year = year; 
     this.month = month; 
     String url = host + restModule + "/"+this.id + "/"+this.year + "/"+this.month; 
     RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); 


     try { 
      status = "Initialized at Url " + builder.getUrl(); 
      Request request = builder.sendRequest(null, new RequestCallback() { 

       public void onError(Request request, Throwable exception) { 
        // Couldn't connect to server (could be timeout, SOP violation, etc.) 
        status = "Error on connecting to Server"; 
       } 


       public void onResponseReceived(Request request, Response response) { 
        if (200 == response.getStatusCode()) { 
         // arrayOfInfo = jsonToJsArray(response.getText());  
         status = "JSON has been Fetched. Result is:" + response.getText(); 
        } else if(0 == response.getStatusCode()) { 
         status = "Error is 0"; 
        } else { 
         status = "Error in JSON Request:" + response.getStatusCode(); 
         //response.getStatusText(); 
        } 
       } 

      }); 

     } catch (RequestException ex) { 
      status = "Error in Request Builder Startup"; 
     } 

    } 

    //get an jso object in array 
    private final native JsArray<InfoJSO> jsonToJsArray(String json) /*-{ 
    return eval(json); 
     }-*/; 

    public JsArray<InfoJSO> getInfoArray(){ 

     return arrayOfInfo; 
    } 

} 

UPDATE:私の問題は、Referring to a non-final variable data inside an inner class .I機構が働いて非同期呼び出しを意識波平と同じです。私はまだ私のRestToInfoSessionクラスの一部ではないラベルを更新するために私のresponse.getText()を渡す方法を知りません....任意のアイデア?

+0

私はgoogle chrome developperツールを使用すると、私のjavascriptが200コードを取得し、その結果が私のjson文字列であることがわかります。しかし、結果は、ブラウザのアプリにラベルに表示されません。 これは私のエントリポイントに入れられたものです: lab3 = new Label(); lab3.setText(新しいRestToInfoSession( "2"、 "2010"、 "12")。getJsonText()); – guiomie

答えて

0

私のjson文字列がnullからxhttpが要求したデータに更新されているかどうかを調べるタイマを1000msごとにチェックしています。これは機能しますが、私はこの問題を解決するよりエレガントな方法があると感じています。

1

RestyGWTプロジェクトの使用を検討してください。 JAXRS JSONリソースをGWT-RPCを使用するのと同じくらい簡単に呼び出すことができます。通常、クライアント側のサーバー側から同じ要求応答DTOを再利用することができます。

関連する問題