我正在尝试在其中一个应用中添加帖子。
我尝试过RestClient,它工作正常。
http://myweb.com/API/Posts/AddPost
{
"TypeId": 1,
"UserId": 9,
"Email": "",
"appLang": "en",
"IsGallery": false,
"Videos": [
{"IsFront": true, "Video": "frontVideo.mp4"},
{"IsFront": false, "Video": "backVideo.mp4"}
],
"key": "abc-def",
"Title": "Post 1",
"Longitude": "",
"userId": "9",
"Id": 0,
"Latitude": "",
"Website": "",
"ContactNo": "",
"Photos": [
{"IsFront": true, "Photo": "frontImage.png"},
{"IsFront": false, "Photo": "backImage.png"}
],
"Hashtag": ""
}
这将在返回帖子时返回我的输出。
[
{
"Id": 25,
"UserId": 9,
"Name": " ",
"UserName": "ff006",
"Phone": "+974 1234566",
"DisplayName": "dff006",
"UserPhoto": "http://myweb.com//Files/UserPhotos/t2z8mryE1KpM9o.jpg",
"Title": "Post 1",
"Hashtag": "",
"Longitude": "",
"Latitude": "",
"Website": "",
"Email": "",
"ContactNo": "",
"FromDateString": "",
"FromDate": null,
"ToDateString": "",
"ToDate": null,
"ViewersCount": 0,
"FavoritesCount": 0,
"IsFollowing": false,
"IsFollowingPending": false,
"IsFavorite": false,
"IsReport": false,
"AdminAdded": false,
"Photo": "http://myweb.com//Files/PostPhotos/frontImage.png",
"AddedOnDate": "18/09/2018 04:07 PM",
"AddedOnFormatted": "04:07 PM",
"AddedOn": "2018-09-18T16:07:39.923",
"PostPhotos": [
{
"PostPhotoId": 10,
"Photo": "http://myweb.com//Files/PostPhotos/frontImage.png",
"IsFront": true
},
{
"PostPhotoId": 11,
"Photo": "http://myweb.com//Files/PostPhotos/backImage.png",
"IsFront": false
}
],
"PostVideos": [
{
"PostVideoId": 9,
"Video": "http://myweb.com//Files/PostVideos/frontVideo.mp4",
"IsFront": true
},
{
"PostVideoId": 10,
"Video": "http://myweb.com//Files/PostVideos/backVideo.mp4",
"IsFront": false
}
]
}
]
现在我正在以编程方式尝试添加,但是发布的照片和视频对象中存在问题。
以下是我尝试过的内容。
我已经创建了如下对象,并尝试如上所述调用http://myweb.com/API/Posts/AddPost
var param = Dictionary<String, Any>()
var photos = [[String: Any]]()
photos.append(["IsFront": true, "Photo": frontPhotoName])
photos.append(["IsFront": false, "Photo": backPhotoName])
var videos = [[String: Any]]()
videos.append(["IsFront": true, "Video": frontVideoName])
videos.append(["IsFront": false, "Video": backVideoName])
param["Id"] = 0
param["appLang"] = "myLang".localized()
param["UserId"] = Int(UserDefaults.standard.string(forKey: "userId")!)
param["TypeId"] = 1
param["Title"] = postTitleTV.text!
param["Hashtag"] = ""
param["Latitude"] = (currLat.containsIgnoringCase(find: "0.00") ? "" : currLat)
param["Longitude"] = (currLong.containsIgnoringCase(find: "0.00") ? "" : currLong)
param["Website"] = websiteTF.text!
param["Email"] = emailTF.text!
param["ContactNo"] = phoneTF.text!
param["IsGallery"] = false
param["Photos"] = photos
param["Videos"] = videos
param["key"] = APIKey
这会添加帖子,但是当我尝试获取帖子时,它不会返回如下的空数组。
[
{
"Id": 26,
"UserId": 9,
"Name": " ",
"UserName": "ff006",
"Phone": "+974 1234566",
"DisplayName": "dff006",
"UserPhoto": "http://myweb.com//Files/UserPhotos/t2z8mryE1KpM9o.jpg",
"Title": "Post 1",
"Hashtag": "",
"Longitude": "",
"Latitude": "",
"Website": "",
"Email": "",
"ContactNo": "",
"FromDateString": "",
"FromDate": null,
"ToDateString": "",
"ToDate": null,
"ViewersCount": 0,
"FavoritesCount": 0,
"IsFollowing": false,
"IsFollowingPending": false,
"IsFavorite": false,
"IsReport": false,
"AdminAdded": false,
"Photo": "",
"AddedOnDate": "18/09/2018 04:13 PM",
"AddedOnFormatted": "04:13 PM",
"AddedOn": "2018-09-18T16:13:58.303",
"PostPhotos": [
],
"PostVideos": [
]
}
]
“照片和视频”对象为空。
"PostPhotos": [
],
"PostVideos": [
]
知道我在做什么错吗?
我正在控制台上打印的数据如下。
["appLang": "en", "UserId": 9, "TypeId": 1,
"Email": "", "IsGallery": false,
"Videos": [["IsFront": true, "Video": "frontVideo.mp4"], ["IsFront": false, "Video": "backVideo.mp4"]],
"key": "abc-def", "Title": "Post 1", "Longitude": "",
"userId": "9", "Id": 0, "Latitude": "", "Website": "", "ContactNo": "",
"Photos": [["IsFront": true, "Photo": "frontImage.png"], ["IsFront": false, "Photo": "backImage.png"]],
"Hashtag": ""]
我还将参数打印为dump(param)
并获得以下输出。
▿ 15 key/value pairs
▿ (2 elements)
- key: "appLang"
- value: "en"
▿ (2 elements)
- key: "UserId"
- value: 9
▿ (2 elements)
- key: "TypeId"
- value: 1
▿ (2 elements)
- key: "Email"
- value: ""
▿ (2 elements)
- key: "IsGallery"
- value: false
▿ (2 elements)
- key: "Videos"
▿ value: 2 elements
▿ 2 key/value pairs
▿ (2 elements)
- key: "IsFront"
- value: true
▿ (2 elements)
- key: "Video"
- value: "frontVideo.mp4"
▿ 2 key/value pairs
▿ (2 elements)
- key: "IsFront"
- value: false
▿ (2 elements)
- key: "Video"
- value: "backVideo.mp4"
▿ (2 elements)
- key: "key"
- value: "abc-def"
▿ (2 elements)
- key: "Title"
- value: "Post 1"
▿ (2 elements)
- key: "Longitude"
- value: ""
▿ (2 elements)
- key: "Id"
- value: 0
▿ (2 elements)
- key: "Latitude"
- value: ""
▿ (2 elements)
- key: "Website"
- value: ""
▿ (2 elements)
- key: "ContactNo"
- value: ""
▿ (2 elements)
- key: "Photos"
▿ value: 2 elements
▿ 2 key/value pairs
▿ (2 elements)
- key: "IsFront"
- value: true
▿ (2 elements)
- key: "Photo"
- value: "frontImage.png"
▿ 2 key/value pairs
▿ (2 elements)
- key: "IsFront"
- value: false
▿ (2 elements)
- key: "Photo"
- value: "backImage.png"
▿ (2 elements)
- key: "Hashtag"
- value: ""
请注意,我正在使用alamofire执行Web服务。
答案 0 :(得分:0)
我刚刚为Alamofire请求添加了ParameterEncoding
的编码(如下所示),
JSONEncoding.default