First, let me start by saying I have got absolutely nothing working with OData4j.
I am building an android app (using Eclipse) that needs to consume some OData from a web service, we have an iPad app already which work fine so I think it's safe to the the web services are ok.
All of the web requests require basic authentication to work, and I am able to make the calls using a browser to confirm that the URL and credentials I am using are valid.
So far I have this code:
ODataConsumer.Builder builder = ODataConsumers.newBuilder(url);
builder.setClientBehaviors(new BasicAuthenticationBehavior(LoginUsername, LoginPassword));
ODataConsumer c = builder.build();
for(OEntity entity : c.getEntities("entry").execute()){
System.out.println(entity.getProperty("name").getValue().toString());
}
Currently, the Eclipse debugger fails on the for
line, presumable during the .execute()
call. The debugger is zero help to me, the only line that looks half-relevant in the LogCat is:
EDIT: After @JohnSpurlock's answer, I got a little further. Same problem overall, but a new error message:
BadRequestException: The request URI is not valid. Since the segment 'Documents' refers to a collection, this must be the last seqment in the request URL. All intermediate segments must refer to a single resource.
There are other messages but mostly to do with threading - it is worth noting that this method is being called from within an AsyncTask, but it seem common to have the threading errors when anything breaks within an AsyncTask
.
A censored and shortened version of my XML result (via a browser) looks like this:
<feed xml:base="http://a.example.com/OData.svc/"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns="http://www.w3.org/2005/Atom">
<entry>
<name>one</name>
</entry>
<entry>
<name>two</name>
</entry>
</feed>
I have worked out the solution.
Firstly, thanks to John Spurlock for getting me a step in the right direction, I needed to first include the correct INTERNET permission in the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
Then comes the problem of querying, essentially the problem is down to my lack of understand out how OData is structured and queried. When I was creating the ODataConsumer
I was passing the full URL query, however I only needed to pass the base .SVC
path.
So rather than this:
ODataConsumer.Builder builder = ODataConsumers.newBuilder("http://a.example.com/OData.svc/Documents");
I did this:
ODataConsumer.Builder builder = ODataConsumers.newBuilder("http://a.example.com/OData.svc");
You then specify the Documents
part when querying for the entites:
for(OEntity entity : c.getEntities("Documents").execute().toList()){
}
My original XML was cut down, and actually contains some properties elements. At first I thought the feed
and entry
elements where entities on their own so I had a problem when trying to query them, but they are infact part of the OData magic and seem to be standard defined elements, so these are handled automatically by OData4j.
Each property can then by queried like so:
String myProperty = entity.getProperty("MyProperty").getValue().toString();