Pass data by hostpage error:JavaScriptObject$ cannot be cast to com.pkg.model

34 views
Skip to first unread message

Alex Luya

unread,
Dec 5, 2012, 3:05:43 AM12/5/12
to google-we...@googlegroups.com
I followed this article to use hostpage to pass an array to client:

https://developers.google.com/web-toolkit/articles/dynamic_host_page   

Currently,I can see follow content in firebug

<html style="overflow: hidden;">
<head>
......
    <script type="text/javascript">
       var rcmdFriends=[{"Name":"Friend-0","Image":"url"}];
    </script>
</head>
......
</html>

Then I tried to use these code to get js variable(a json array actually) from hostpage and print it to user:

//get array from host page
private native JsArrayExt<People> getRecommendedFriends()/*-{
    return $wnd.rcmdFriends;
}-*/;

@Override
public void onModuleLoad()
{
    final FlowPanel fPanel = new FlowPanel();
    JsArrayExt<People> channels = getRecommendedFriends();
    for (int i = 0, len = channels.length(); i < len; i++)
    {
                 //"print" name to user
         fPanel.add(new Label(channels.get(i).getName()));
    }
    RootPanel.get().add(fPanel);
}

    //model definition
    @SingleJsoImpl(PeopleImpl.class)
    public interface People extends HasName
    {
        String getImage();
        void setImage(String Image);
    }   

But got this eror:

java.lang.ClassCastException: com.google.gwt.core.client.JavaScriptObject$ cannot be cast to com.pkg.People

Strangely,I can already see the length of "channels" is 1,and why do I get this casting error?How to solove this problem?

Sebastián Gurin

unread,
Dec 5, 2012, 11:31:57 AM12/5/12
to google-we...@googlegroups.com
I think you have it wrong. I would do this, first, People must be a JavaScriptObject not an interface:

public class People extends JavaScriptObject {
protected People(){}

public native final String image() /*-{
return this["image"];
}-*/;

public native final People image(String val) /*-{
this["image"] = val;
return this;
}-*/;
}


Then, I think you are wrong also in getRecommendedFriends(). It needs to be final and to return a JsArray<People>

private native final JsArray<People> getRecommendedFriends()/*-{
    return $wnd.rcmdFriends;
}-*/;

Other stuff I think it is OK. Than should work.
Reply all
Reply to author
Forward
0 new messages