【问题标题】:getEntity call results in crash (using odata4j on a WCF service)getEntity 调用导致崩溃(在 WCF 服务上使用 odata4j)
【发布时间】:2011-06-03 18:08:25
【问题描述】:

我正在我的 android 应用程序中尝试使用 odata4j 从可从 WCF 服务访问的数据库中检索数据。

 ODataConsumer co = ODataConsumer.create("http://xxx.xx.xx.xxx:xxxx/Users");
 for(OEntity user : co.getEntities("Users").execute())
 {
      // do stuff
 }

但是,这会在调用 getEntities 时崩溃。我也尝试过各种其他调用,例如

 Enumerable<OEntity> eo = co.getEntities("Users").execute();            
 OEntity users = eo.elementAt(0);

但是,这也会在 eo.elementAt(0) 处崩溃。

logcat 什么也没告诉我,调用堆栈似乎在 ActivityThread.performLaunchActivity 处暂停。

另一方面,在我的网络浏览器中输入“http://localhost:xxxx/Users”按预期工作,并以 xml 格式返回我的数据库中的用户。

关于如何调试它的任何想法?

【问题讨论】:

    标签: android wcf odata odata4j


    【解决方案1】:

    记录所有 http 请求/响应:

    ODataConsumer.dump.all(true);
    

    传递给消费者 .create 调用的 uri 应该是服务根。例如.create("http://xxx.xx.xx.xxx:xxxx/");否则你的代码看起来不错。

    注意 Enumerable 的行为类似于 .net 类型 - 枚举被推迟到访问。如果您打算多次索引结果,我建议您先调用 .toList()。

    告诉我你发现了什么。

    希望对您有所帮助,
    - 约翰

    【讨论】:

    • 感谢您的提示。在 ODataConsumer.dump.all(true) 之后,所有 http 请求\响应在哪里记录?是的,我在 create 方法中修改了 url,但它仍然崩溃:(
    【解决方案2】:

    我猜电话应该是:

    ODataConsumer co = ODataConsumer.create("http://xxx.xx.xx.xxx:xxxx");
    for(OEntity user : co.getEntities("Users").execute())
    {
         // do stuff
    }
    

    create 定义了您要连接的服务,但 Users 是您要查询的资源。

    【讨论】:

    • 啊,我真傻 - 是的,我最初尝试过这个。我已经从创建调用中删除了“用户”,但它仍然挂在 getEntities :(
    • 首先检查一下——您的安卓应用程序甚至可以访问该地址吗?否则您可以使用 Fiddler 之类的东西并比较来自浏览器的请求和来自 android 的请求吗?
    • 是的,如果我改用 HttpGet,我的 Android 可以访问该地址。不幸的是,当我跨过 getEntities 循环时,Fiddler 没有向我显示任何新项目。
    • 只是一个愚蠢的尝试。如果在 url 中添加斜杠怎么办?
    • 尝试在 create 方法的 url 中添加尾随 '/',不幸的是仍然没有运气
    【解决方案3】:

    你可以试试这个方法吗?

    OEntity oEntity;     
             OQueryRequest<OEntity>  oQueryRequest=  oDataJerseyConsumer.getEntities(entityName);
             List<OEntity>  list=  oQueryRequest.execute().toList();
    
             for (OEntity o : list) {
                    List<OProperty<?>> props = o.getProperties();
                    for (OProperty<?> prop : props) {
                        System.out.println(prop.getValue().toString());
    
                    }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多