如何使用Java查询Odata提要

如何使用Java查询Odata提要,java,android,odata,visual-studio-lightswitch,Java,Android,Odata,Visual Studio Lightswitch,我是oData服务的新手,希望从lightswitch oData提要中查询(即选择特定字段/记录)。在Java中有没有一种简单的方法可以做到这一点。我听说oData是Atom“兼容”的——如果是这样,谁能让我开始呢 我的提要如下所示: <?xml version="1.0" encoding="utf-8"?><feed xml:base="http://my.ip.address/test5/testXPressDBData.svc/" xmlns="http://www.

我是oData服务的新手,希望从lightswitch oData提要中查询(即选择特定字段/记录)。在Java中有没有一种简单的方法可以做到这一点。我听说oData是Atom“兼容”的——如果是这样,谁能让我开始呢

我的提要如下所示:

<?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>
http://pocketsize/test5/testXPressDBData.svc/dogs/dogs2012-07-24T00:29:39Zhttp://my.ip.address/test5/testXPressDBData.svc/dogs(1) 2012-07-24T00:29:39Z1hammy325brownhttp://my.ip.address/test5/testXPressDBData.svc/dogs(2) 2012-07-24T00:29:39Z2rufus510blondehttp://my.ip.address/test5/testXPressDBData.svc/dogs(3)2012-07-24T00:29:39Z3jenkins15spottedhttp://my.ip.address/test5/testXPressDBData.svc/dogs(4) 2012-07-24T00:29:39Z4billy1223Purplehttp://my.ip.address/test5/testXPressDBData.svc/dogs(5) 2012-07-24T00:29:39Z5BURGARIA23brownhttp://my.ip.address/test5/testXPressDBData.svc/dogs(6) 2012-07-24T00:29:39Z6中国22粉红色

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


看看OData4J——它为OData提供了很多基本支持,并帮助从提要中具体化实体

示例代码来自:

package org.odata4j.examples.consumers;
导入java.util.List;
导入org.odata4j.consumer.ODataConsumer;
导入org.odata4j.core.OEntity;
导入org.odata4j.core.OProperty;
导入org.odata4j.examples.AbstractExample;
导入org.odata4j.examples.ODataEndpoints;
公共类NetflixConsumerExample扩展了抽象示例{
公共静态void main(字符串[]args){
NetflixConsumerExample=new NetflixConsumerExample();
示例.run(args);
}
私有无效运行(字符串[]args){
ODataConsumer c=this.rtFacde.create(ODataEndpoints.NETFLIX,null,null);
//找到Morgan Spurlock的netflix id
int morganSpurlockId=c.getEntities(“People”).filter(“substringof('Spurlock',Name)”).execute().first().getProperty(“Id”,Integer.class).getValue();
//查找并打印他演过的所有片子
List titlesActedIn=c.getEntities(“People”).nav(morganSpurlockId,“titlesActedIn”).execute().toList();
用于(实体名称:titlesActedIn){
for(OProperty p:title.getProperties()){
报告(“%s:%s”,p.getName(),p.getValue());
}
报告(“\n”);
}
报告(“计数:+titleActedIn.size());
} 
} 

Hi Mark-谢谢,是的,我尝试了这个示例并加载了oData4j,但现在我在读取我的Lightswitch odata提要时遇到了问题…希望能尽快解决,但与此同时,我想我自己也会尝试一下你遇到了什么问题?我确信他们/我们想知道,以便我们能够确定是否需要修复某些问题。嗨,马克-我已经向oData4j集团发布了这个问题(他们真的很有帮助),但到目前为止我还无法阅读Lightswitch。这个问题的链接是:再次您好-但是我已经能够读取/插入WCF odata提要,仍然在进行删除/更新…我有一个“接收到的身份验证质询为空”错误。非常感谢您的帮助。
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());

  } 
}