【问题标题】:post request not adding inner array of an object发布请求不添加对象的内部数组
【发布时间】:2018-09-18 13:20:51
【问题描述】:

我正在尝试在其中一个应用中添加帖子。

我用 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上面提到的webservice

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 服务。

【问题讨论】:

  • 如果你检查服务器端是否有任何照片或视频上传?
  • @Tobi :不,如果添加它们,它会以 json 格式出现......
  • 我的意思是这不可能是服务器问题
  • @Tobi :我创建对象的方式好吗?为什么 swift 不将字典显示为 { }?
  • @Tobi : 不是服务器问题,因为 RestClient 中的相同数据工作正常

标签: swift http-post swift4 alamofire


【解决方案1】:

我刚刚为 Alamofire 请求添加了 ParameterEncoding 的编码(如下所示),它起作用了

JSONEncoding.default

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 2012-09-12
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多