简体   繁体   中英

How to Consume OData from Android?

I have WebAPI : http://some.com:39760/odata/DriverInfoes

it shows following when i hit the link

{
  "odata.metadata":"http://some.com:39760/odata/$metadata#DriverInfoes","value":[
    {
      "DriverID":"1","Name":"Faria","Phone":"ss","Email":"ss","Time":null
    }
  ]
}

in android i added odata4j-0.7.0-clientbundle for consuming Odata

private class DownloadWebPageTask extends AsyncTask<Void, Void, OEntity> {
    protected OEntity doInBackground(Void... params) {

        String serviceUrl = "http://some.com:39760/odata/DriverInfoes ";
        ODataConsumer consumer = ODataConsumers.create(serviceUrl);

        return (OEntity) consumer.getEntities("DriverInfoes").execute(); //getting exception here
    }

    @Override
    protected void onPostExecute(OEntity consumer) {
        Log.v("mango", consumer.getType().getFullyQualifiedTypeName());
    }

}

i am getting exception on

return (OEntity) consumer.getEntities("DriverInfoes").execute();

and in my oncreate method i wrote

DownloadWebPageTask task = new DownloadWebPageTask();
task.execute();

but i am not having any luck. i think i am doing something wrong ! can anybody point it to me?

its JSON data... Try To Parse With JSONObject And JSONArray...

Your Data.

{
    "odata.metadata": "http://some.com:39760/odata/$metadata#DriverInfoes",
    "value": [
        {
            "DriverID": "1",
            "Name": "Faria",
            "Phone": "ss",
            "Email": "ss",
            "Time": null
        }
    ]
}

Parse Your Data with following code...

JSONObject JObj=new JSONObject (jsonresult);
String metadata= JObj.getString("odata.metadata");

JSONArray array_list = JObj.getJSONArray("value");

//getting order Id from **order_list array**
for(int i=0;i<array_list.length();i++)
{
         String DriverID= array_list.getJSONObject(i).getString("DriverID");
         String Name= array_list.getJSONObject(i).getString("Name");
         String Phone= array_list.getJSONObject(i).getString("Phone");
         String Email= array_list.getJSONObject(i).getString("Email");
         String Time= array_list.getJSONObject(i).getString("Time");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM