使用GWT解析XML或JSON的挫败感。

时间:2022-12-01 11:23:24

For the past couple of days, I have been trying to get GWT to interpret either a JSONValue or a string of XML that gets passed back from the server (using PHP).

在过去的几天中,我一直试图让GWT解释从服务器(使用PHP)返回的JSONValue或XML字符串。

I'm pretty frustrated, because I can't seem to get anything to work. For the XML, I have confirmed that the String passed from PHP to GWT is a correct XML string. However, when I try to parse the XML, I get a bunch of null errors.

我很沮丧,因为我似乎找不到工作。对于XML,我已经确认从PHP传递到GWT的字符串是正确的XML字符串。但是,当我尝试解析XML时,会得到一堆空错误。

With the JSON, I get the following from the PHP:

使用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}]}

I have no idea why the values are NULL, but this is how the GWT looks for the JSON:

我不知道为什么这些值为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);

 }

Does anyone know what I am doing wrong with the JSON? I am doing json_encode($array) in the PHP, and I have no idea how to break it down in GWT.

有人知道我对JSON做错了什么吗?我正在PHP中执行json_encode($array),我不知道如何在GWT中分解它。

Unfortunately, there aren't many examples online, either...

不幸的是,网上也没有太多的例子……

Thanks!

谢谢!

1 个解决方案

#1


1  

It looks like your first problem is server-side (PHP) and has nothing to do with GWT. Just going to your page (http://www.kbehr.com/calendar/view_duty.php) should return some kind of data, not a whole bunch of nulls (probably).

看起来您的第一个问题是服务器端(PHP),与GWT无关。只需访问页面(http://www.kbehr.com/calendar/view_duty.php),就会返回某种数据,而不是一堆空值(可能)。

As for how to use the JSON parser in GWT, go here and start reading at "2. Manipulating JSON data in the client-side code"

至于如何在GWT中使用JSON解析器,请转到这里并从“2”开始阅读。在客户端代码中操作JSON数据"

#1


1  

It looks like your first problem is server-side (PHP) and has nothing to do with GWT. Just going to your page (http://www.kbehr.com/calendar/view_duty.php) should return some kind of data, not a whole bunch of nulls (probably).

看起来您的第一个问题是服务器端(PHP),与GWT无关。只需访问页面(http://www.kbehr.com/calendar/view_duty.php),就会返回某种数据,而不是一堆空值(可能)。

As for how to use the JSON parser in GWT, go here and start reading at "2. Manipulating JSON data in the client-side code"

至于如何在GWT中使用JSON解析器,请转到这里并从“2”开始阅读。在客户端代码中操作JSON数据"