2009-07-11 98 views
2

在過去的幾天,我一直在試圖讓GWT解釋無論是JSONValue或會從服務器(使用PHP)傳遞迴XML字符串。挫折解析XML或JSON用GWT

我很沮喪,因爲我似乎無法得到任何工作。對於XML,我已經確認從PHP傳遞給GWT的字符串是正確的XML字符串。但是,當我嘗試解析XML時,出現一堆空錯誤。

隨着JSON,我從PHP以下內容:

Value: {"item":[{"ID":null, "Primary":null, "Secondary":null, "Date":null, "Region":null},{"ID":null, "Primary":null, "Secondary":null, "Date":null, "Region":null},{"ID":null, "Primary":null, "Secondary":null, "Date":null, "Region":null}]} 

我不知道爲什麼值是NULL,但這是GWT的外觀爲JSON:

public void onChange(Widget sender) { 
    lb.setText("Date selected: " + calendar.getDate()); 
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
    String current = df.format(calendar.getDate()); 

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode("http://www.kbehr.com/calendar/view_duty.php")); 

try { 
    builder.sendRequest(current, new RequestCallback(){ 
    public void onError(Request request, Throwable exception) { 
     requestFailed(exception); 
    } 

    public void onResponseReceived(Request request, Response response) { 

     String responseText = response.getText(); 
     try { 
      JSONValue jsonValue = JSONParser.parse(responseText); 
      processJSON(jsonValue); 
     } catch (Exception e) { 
      Window.alert("Error: " + e); 
     } 

    }}); 
}catch (RequestException ex) { 
    requestFailed(ex); 
}  

}}); 
    fp.add(calendar); 
    fp.add(lb); 
} 

public void processJSON(JSONValue messageXml) { 

vp.clear(); 
vp.add(new Label("Value: " + messageXml)); 
RootPanel.get("slot2").add(vp); 

} 

有誰知道我在做什麼錯的JSON?我做json_encode($陣列)在PHP,我不知道如何打破它在GWT。

不幸的是,網上沒有太多例子,要麼...

謝謝!

回答

1

它看起來像你的第一個問題是服務器端(PHP),並有無關GWT。只是要你的頁面(http://www.kbehr.com/calendar/view_duty.php)應返回某種類型的數據,而不是一大堆的空值(可能)的。

至於如何使用JSON解析器中GWT,去here,並開始在

「2.客戶端代碼操縱JSON數據」讀書