iOS使用OData 网络请求获取数据

楼主也是第一次接触到OData.最初看到OData 这个词时 ,楼主也是一脸懵逼.
在维基百科 是这样定义OData的.
OData: 英文全名是Open Data Protocol(OData),它是一个开源的协定,以简单和标准的方法,来建造或消除可查询和可操作的RESTful API。它创始于2007年的微软公司.简而言之, OData协议,是以数据模型的操作为核心,通过相关工具,能快速构建出标准化的Restful 的API.
看完这些我估计大家还是糊里糊涂的.其实简单的来说吧 ,就是查询数据一般就用get,删除用delete ,修改用patch , 创建用post.

请求权限 基础权限(Basic Authorization) 即只需要提供用户名和密码

同样对现有数据的修改,PATCH操作和PUT操作不同之处:PUT操作提交的请求数据体,必须包含需要修改的数据和以前没有修改的数据字段,PUT操作会根据新提交的各个字段进行比较,发现有值不同的地方,就会进行更改。如果PATCH, 只需要提供需要修改的字段和对应的新的数值。

在OData 协议提供的典型的查询参数:
$count. 返回查询结果的数量
$expand. 将查询的结果数据模型进行扩展,扩展到相关联的数据模型。
$filter. 查询结果进行过滤
$format. 查询结果设置数据格式:比如xml,json.
$inlinecount.
$orderby. 查询结果按照一定规则排序
$search. 设置全文搜索的条件
$select.
$skip
$skiptoken
$top 获取前面多少条

$filter关键字,可以连接后面的条件表达式,格式为: $filter = <fieldname> <operator> <value>, 比如 $filter= Company eq 'xx有限公司'。

“expand”关键字,表示返回的数据模型除了根节点的数据,还包含了相关关联节点上的数据。

“$search”关键字,可以对数据里面的所有开启了“搜索功能”的字段,进行全文搜索

  1. get 例子
    请求url = https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection?$format=json&$filter=Name eq '测试111111'
{
    "d": {
        "results": [
            {
                "__metadata": {
                    "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')",
                    "type": "c4codata.LeanLead",
                    "etag": "W/\"datetimeoffset'2018-01-04T07%3A13%3A45.0486160Z'\""
                },
                "ETag": "/Date(1515050025048)/",
                "ObjectID": "00163E217B181EE7BCA3D9913A1DAFFD",
                "GroupCode": "",
                "ID": "1695",
                "OriginTypeCode": "Z02",
                "PriorityCode": "3",
                "EndDate": "/Date(1517616000000)/",
                "StartDate": "/Date(1515024000000)/",
                "QualificationLevelCode": "",
                "ResultReasonCode": "",
                "ApprovalStatusCode": "",
                "ConsistencyStatusCode": "2",
                "UserStatusCode": "01",
                "SurveyTotalScoreValue": 0,
                "CreationDateTime": "/Date(1515050025048)/",
                "DistributionChannelCode": "",
                "DivisionCode": "",
                "SalesGroupID": "",
                "SalesGroupUUID": null,
                "SalesOfficeID": "",
                "SalesOfficeUUID": null,
                "SalesOrganisationID": "",
                "SalesOrganisationUUID": null,
                "SalesTerritoryID": "",
                "SalesTerritoryUUID": null,
                "SalesTerritoryName": "",
                "OwnerPartyIDSales": "",
                "OwnerPartyUUIDSales": null,
                "OwnerSalesName": "",
                "MarketingUnitPartyID": "",
                "MarketingUnitPartyUUID": null,
                "MarketingUnitName": "",
                "AccountPartyID": "000000000000000000000000000000000000000000000000000600000026",
                "AccountPartyUUID": "00163E0C-2A6D-1ED6-A0AC-033C2B74944B",
                "AccountPartyName": "八佳电器 八佳电气",
                "ContactID": "",
                "ContactUUID": null,
                "ContactName": "",
                "OwnerPartyID": "000000000000000000000000000000000000000000000000008000000030",
                "OwnerPartyUUID": "00163E0C-2A6D-1EE6-9FC8-8A8E89C47BBA",
                "OwnerPartyName": "凯 胡",
                "SalesUnitPartyID": "",
                "SalesUnitPartyUUID": null,
                "SalesUnitName": "",
                "ExpectedRevenueAmount": "0.000000",
                "ExpectedRevenueCurrencyCode": "",
                "Name": "测试111111",
                "NameLanguageCode": "ZH",
                "ExternalID": "",
                "LifeCycleStatusCode": "1",
                "SpecialCommitteeID": "000000000000000000000000000000000000000000000000001000000020",
                "SpecialCommitteeName": "固定收益类",
                "RespEmployeeID": "000000000000000000000000000000000000000000000000008000000338",
                "RespEmployeeName": "",
                "LeadOfferEmployeeID": "000000000000000000000000000000000000000000000000008000000339",
                "LeadOfferEmployeeName": "",
                "CreatorName": "曹 为",
                "IsConvertoOpp": false,
                "OppConversionedName": "",
                "OppConversionedID": "",
                "Company": "八佳电器",
                "ContactFirstName": "",
                "ContactLastName": "",
                "ContactPhone": "",
                "ContactMobile": "",
                "ContactEMail": "",
                "AccountCity": "洛阳市",
                "AccountState": "180",
                "AccountCountry": "CN",
                "CampaignID": "",
                "EmployeeResponsibleUUID": "00163E0C-2A6D-1EE6-9FC8-8A8E89C47BBA",
                "CompanySecondName": "八佳电气",
                "CompanyThirdName": "",
                "CompanyFourthName": "八佳电器 八佳电气-作废",
                "OrganisationAccountABCClassificationCode": "A",
                "OrganisationAccountIndustrialSectorCode": "",
                "OrganisationAccountContactAllowedCode": "",
                "ContactMiddleName": "",
                "ContactGenderCode": "",
                "ContactAcademicTitleCode": "",
                "ContactAdditionalAcademicTitleCode": "",
                "ContactAllowedCode": "",
                "ContactFormOfAddressCode": "",
                "ContactNickName": "",
                "ContactMaritalStatusCode": "",
                "BusinessPartnerRelationshipBusinessPartnerFunctionTypeCode": "",
                "BusinessPartnerRelationshipBusinessPartnerFunctionalAreaCode": "",
                "BusinessPartnerRelationshipEngagementScoreNumberValue": "0 ",
                "BusinessPartnerRelationshipContactVIPReasonCode": "",
                "ContactCorrespondenceLanguageCode": "",
                "IndividualCustomerFormOfAddressCode": "",
                "IndividualCustomerAcademicTitleCode": "",
                "IndividualCustomerGivenName": "",
                "IndividualCustomerFamilyName": "",
                "IndividualCustomerMiddleName": "",
                "IndividualCustomerGenderCode": "",
                "IndividualCustomerContactAllowedCode": "",
                "IndividualCustomerMaritalStatusCode": "",
                "IndividualCustomerABCClassificationCode": "",
                "IndividualCustomerNationalityCountryCode": "",
                "IndividualCustomerBirthDate": null,
                "IndividualCustomerOccupationCode": "",
                "IndividualCustomerInitialsName": "",
                "IndividualCustomerNamePrefixCode": "",
                "IndividualCustomerNonVerbalCommunicationLanguageCode": "",
                "IndividualCustomerPhone": "",
                "IndividualCustomerEMail": "",
                "IndividualCustomerMobile": "",
                "IndividualCustomerFax": "",
                "IndividualCustomerAddressHouseID": "",
                "IndividualCustomerAddressStreetName": "",
                "IndividualCustomerAddressCity": "",
                "IndividualCustomerAddressCountry": "",
                "IndividualCustomerAddressState": "",
                "IndividualCustomerAddressPostalCode": "",
                "IndividualCustomerAddressCountyName": "",
                "AccountPostalAddressElementsStreetPostalCode": "",
                "AccountPostalAddressElementsCountyName": "",
                "AccountPostalAddressElementsStreetPrefix": "",
                "AccountPostalAddressElementsAdditionalStreetPrefixName": "",
                "AccountPostalAddressElementsStreetName": "",
                "AccountPostalAddressElementsStreetSufix": "",
                "AccountPostalAddressElementsAdditionalStreetSuffixName": "",
                "AccountPostalAddressElementsHouseID": "",
                "AccountPostalAddressElementsPOBoxID": "",
                "AccountPhone": "",
                "AccountEMail": "",
                "AccountMobile": "",
                "AccountFax": "",
                "AccountWebsite": "",
                "AccountLatitudeMeasureUnitCode": "",
                "AccountLatitudeMeasure": "0.00000000000000",
                "AccountLongitudeMeasureUnitCode": "",
                "AccountLongitudeMeasure": "0.00000000000000",
                "AccountCorrespondenceLanguageCode": "",
                "AccountPreferredCommunicationMediumTypeCode": "",
                "ContactFunctionalTitleName": "",
                "ContactDepartmentName": "",
                "ContactEMailUsageDeniedIndicator": "",
                "ContactFacsimileFormattedNumberDescription": "",
                "ContactBuildingID": "",
                "ContactFloorID": "",
                "ContactRoomID": "",
                "AccountLegalForm": "Z2",
                "AccountDUNS": "",
                "Note": "",
                "AccountNote": "",
                "ContactNote": "",
                "GroupCodeText": "",
                "OriginTypeCodeText": "中介机构渠道",
                "PriorityCodeText": "普通",
                "QualificationLevelCodeText": "",
                "ResultReasonCodeText": "",
                "ApprovalStatusCodeText": "",
                "ConsistencyStatusCodeText": "不一致",
                "UserStatusCodeText": "未完成",
                "DistributionChannelCodeText": "",
                "DivisionCodeText": "",
                "ExpectedRevenueCurrencyCodeText": "",
                "NameLanguageCodeText": "中文",
                "LifeCycleStatusCodeText": "未处理",
                "LeanLeadAttachmentFolder": {
                    "__deferred": {
                        "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadAttachmentFolder"
                    }
                },
                "LeanLeadSalesAndMarketingTeam": {
                    "__deferred": {
                        "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadSalesAndMarketingTeam"
                    }
                },
                "LeanLeadExternalParty": {
                    "__deferred": {
                        "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadExternalParty"
                    }
                },
                "LeanLeadItem": {
                    "__deferred": {
                        "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadItem"
                    }
                },
                "LeanLeadContact": {
                    "__deferred": {
                        "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadContact"
                    }
                },
                "LeanLeadBusinessTransactionDocumentReference": {
                    "__deferred": {
                        "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadBusinessTransactionDocumentReference"
                    }
                }
            }
        ]
    }
}

可以对 LeanLeadSalesAndMarketingTeam, LeanLeadContact等子节点展开扩展
例如 https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection?$format=json&$filter=Name eq '测试111111'&$expand=LeanLeadSalesAndMarketingTeam

 "LeanLeadSalesAndMarketingTeam": [
                    {
                        "__metadata": {
                            "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadSalesAndMarketingTeamCollection('00163E217B181EE7BCA3D9913A1D8FFD')",
                            "type": "c4codata.LeanLeadSalesAndMarketingTeam",
                            "etag": "W/\"datetimeoffset'2018-01-04T07%3A13%3A45.0486160Z'\""
                        },
                        "ETag": "/Date(1515050025048)/",
                        "ObjectID": "00163E217B181EE7BCA3D9913A1D8FFD",
                        "ParentObjectID": "00163E217B181EE7BCA3D9913A1DAFFD",
                        "MainIndicator": true,
                        "PartyID": "8000000030",
                        "PartyUUID": "00163E0C-2A6D-1EE6-9FC8-8A8E89C47BBA",
                        "RoleCode": "39",
                        "PartyName": "凯 胡",
                        "Phone": "",
                        "EMail": "",
                        "RoleCodeText": "承揽负责人",
                        "LeanLead": {
                            "__deferred": {
                                "uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadSalesAndMarketingTeamCollection('00163E217B181EE7BCA3D9913A1D8FFD')/LeanLead"
                            }
                        }
                    }
]
                   

2
OData数据的编辑、新建和删除操作,是“POST/PUT/PATCH/DELETE”的请求方式,权限校验方式,除了需要用户名和密码,还需要x-csrf-token.
先get 请求 请求时设置请求的Header, 包含这样的参数:x-csrf-token:fetch

当返回的状态编码为200时,表示请求成功,且x-csrf-token的值

post put patch delete 等操作都要先去get 获取到x-csrf-token

获取x-csrf-token


B8FBC061-13C5-4CF0-B260-6EA1F606ED7A.png

把获取到Token 放header里面


132AFEFCB242BB3D75D6A2E8699C9DDF.png

请求body 的设置 选择raw 模式 和 json类型


AFEDF0F6-0D13-4978-9605-7C6CF956C443.png

其他的 delete put 都类似.

PS:
(
如果使用AFNetWorking请求的话 ,
注意使用 AFHTTPSessionManager* man=[AFURLSessionManager manager];
man.requestSerializer=[AFJSONRequestSerializer serializer];
)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容