【问题标题】:How to interrogate an Odata feed using Java如何使用 Java 查询 Odata 提要
【发布时间】:2012-07-22 07:01:24
【问题描述】:

我对 oData 服务很陌生,想从 lightswitch Odata 提要中查询(即挑选特定字段/记录)。有没有一种简单的方法可以在 Java 中做到这一点。我听说 oData 是“兼容的” - 如果是这样,任何人都可以让我开始。

我的提要如下所示:

<?xml version="1.0" encoding="utf-8"?><feed xml:base="http://my.ip.address/test5/testXPressDBData.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><id>http://pocketsize/test5/testXPressDBData.svc/dogs/</id><title type="text">dogs</title><updated>2012-07-24T00:29:39Z</updated><link rel="self" title="dogs" href="dogs" /><entry m:etag="W/&quot;3&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(1)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(1)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">1</d:id><d:name>hammy</d:name><d:age m:type="Edm.Int32">3</d:age><d:weight m:type="Edm.Single">25</d:weight><d:colour>brown</d:colour></m:properties></content></entry><entry m:etag="W/&quot;5&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(2)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(2)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">2</d:id><d:name>rufus</d:name><d:age m:type="Edm.Int32">5</d:age><d:weight m:type="Edm.Single">10</d:weight><d:colour>blonde</d:colour></m:properties></content></entry><entry m:etag="W/&quot;1&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(3)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(3)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">3</d:id><d:name>jenkins</d:name><d:age m:type="Edm.Int32">1</d:age><d:weight m:type="Edm.Single">5</d:weight><d:colour>spotted</d:colour></m:properties></content></entry><entry m:etag="W/&quot;12&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(4)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(4)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">4</d:id><d:name>billy</d:name><d:age m:type="Edm.Int32">12</d:age><d:weight m:type="Edm.Single">23</d:weight><d:colour>Purple</d:colour></m:properties></content></entry><entry m:etag="W/&quot;2&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(5)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(5)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">5</d:id><d:name>Bulgaria</d:name><d:age m:type="Edm.Int32">2</d:age><d:weight m:type="Edm.Single">3</d:weight><d:colour>brown</d:colour></m:properties></content></entry><entry m:etag="W/&quot;2&quot;"><id>http://my.ip.address/test5/testXPressDBData.svc/dogs(6)</id><category term="LightSwitchApplication.dog" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="dog" href="dogs(6)" /><title /><updated>2012-07-24T00:29:39Z</updated><author><name /></author><content type="application/xml"><m:properties><d:id m:type="Edm.Int32">6</d:id><d:name>china</d:name><d:age m:type="Edm.Int32">2</d:age><d:weight m:type="Edm.Single">2</d:weight><d:colour>pink</d:colour></m:properties></content></entry></feed>

【问题讨论】:

    标签: java android odata visual-studio-lightswitch


    【解决方案1】:

    看看 OData4J - 它为 OData 提供了许多基本支持,并有助于从提要中具体化实体。

    来自http://code.google.com/p/odata4j/source/browse/odata4j-fit/src/test/java/org/odata4j/examples/consumers/NetflixConsumerExample.java?name=0.6的示例代码:

    package org.odata4j.examples.consumers;
    
    import java.util.List;
    
    import org.odata4j.consumer.ODataConsumer;
    import org.odata4j.core.OEntity;
    import org.odata4j.core.OProperty;
    import org.odata4j.examples.AbstractExample;
    import org.odata4j.examples.ODataEndpoints;
    
    public class NetflixConsumerExample extends AbstractExample {
    
      public static void main(String[] args) {
        NetflixConsumerExample example = new NetflixConsumerExample();
        example.run(args);
      }
    
      private void run(String[] args) {
        ODataConsumer c = this.rtFacde.create(ODataEndpoints.NETFLIX, null, null);
    
        // locate the netflix id for Morgan Spurlock
        int morganSpurlockId = c.getEntities("People").filter("substringof('Spurlock',Name)").execute().first().getProperty("Id", Integer.class).getValue();
    
        // lookup and print all titles he's acted in
        List<OEntity> titlesActedIn = c.getEntities("People").nav(morganSpurlockId, "TitlesActedIn").execute().toList();
        for (OEntity title : titlesActedIn) {
          for (OProperty<?> p : title.getProperties()) {
            report("%s: %s", p.getName(), p.getValue());
          }
          report("\n");
        }
        report("count: " + titlesActedIn.size());
    
      } 
    } 
    

    【讨论】:

    • 嗨,马克-谢谢,是的,我已经尝试过这个示例并加载了 oData4j,但现在我在读取我的 Lightswitch odata 提要时遇到问题...希望尽快解决这个问题,但在与此同时,我以为我自己会去一趟
    • 您遇到了什么问题?我确定他们/我们想知道,这样我们就可以确定是否需要修复某些问题。
    • 嗨,马克 - 我已经在 oData4j 小组(他们真的很有帮助)上发布了这个问题,但我还无法阅读 Lightswitch。这个问题的链接是:groups.google.com/forum/?fromgroups#!topic/odata4j-discuss/…
    • 再次嗨 - 但是我已经能够读取/插入 WCF odata 提要,仍在进行删除/更新...我有一个“收到的身份验证挑战为空”错误。非常感谢任何帮助。
    【解决方案2】:

    只需通过 SaxParser 运行它。然后喝点水。

    http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多