2016-08-05 55 views
1

我使用下面的代碼從服務器獲取json數據,它返回錯誤。Alamofire返回錯誤

代碼:

func getPosts(page : Int, count : Int,completionHandler: (responseObject: ResponseModel) ->()){ 
    let header = ["Content-Type": "application/json"] 
    let responseModel = ResponseModel() 
    var posts = [StoryModel]() 
    var comments = [CommentModel]() 
    var customFields = [CustomFieldModel]() 
    let medium = ImageTypeModel() 
    let high = ImageTypeModel() 
    var postCount = [String]() 
    var categories = [CategoryModel]() 
    manager!.request(.GET, "http://www.anonews.co?json=get_recent_posts", parameters: ["page": page,"count" :count],headers: header) 
     .responseJSON { response in 
      switch response.result { 
      case .Success: 
       if let value = response.result.value { 
        let json = JSON(value) 
        print(json) 
        responseModel.status = json["status"].stringValue 
        responseModel.count = json["count"].intValue 
        responseModel.count_total = json["count_total"].intValue 
        responseModel.pages = json["pages"].intValue 

        for item in json["posts"].arrayValue { 
         let storymodel = StoryModel(); 
         storymodel.comment_count = item["comment_count"].stringValue 
         storymodel.content = item["content"].stringValue 
         storymodel.excerpt = item["excerpt"].stringValue 
         storymodel.id = item["id"].intValue 
         storymodel.imageUrl = item["url"].stringValue 
         storymodel.title_plain = item["title_plain"].stringValue 
         storymodel.thumbnail = item["thumbnail"].stringValue 
         medium.url = item["thumbnail_images"]["medium_large"]["url"].stringValue 
         high.url = item["thumbnail_images"]["mvp-medium-thumb"]["url"].stringValue 
         storymodel.medium_large = medium 
         storymodel.mvp_medium_thumb = high 

         for category in json["posts"]["categories"].arrayValue { 
          let categoryModel = CategoryModel() 
          categoryModel.id = category["id"].intValue 
          categoryModel.title = category["title"].stringValue 
          categories.append(categoryModel) 
         } 
         storymodel.categories = categories 

         for commentobject in json["posts"]["comments"].arrayValue { 
          let comment = CommentModel() 
          comment.content = commentobject["content"].stringValue 
          comment.name = commentobject["name"].stringValue 
          comments.append(comment) 
         } 
         storymodel.comments = comments 

         for customFieldObject in json["posts"]["custom_fields"].arrayValue { 
          let customField = CustomFieldModel() 
          for post_count in customFieldObject["post_views_count"].arrayValue { 
           let count = post_count["post_views_count"].stringValue 
           postCount.append(count) 
          } 
          customField.post_views_count = postCount 
          customFields.append(customField) 
         } 
         storymodel.custom_fields = customFields 
         posts.append(storymodel) 
        } 
        responseModel.posts = posts 
        completionHandler(responseObject: responseModel) 
       } 
      case .Failure(let error): 
       print(error) 
      } 


    } 
} 

以下是錯誤消息: 錯誤域= NSCocoaErrorDomain代碼= 3840 「無效字符周圍0值」 UserInfo = {NSDebugDescription =字符0周圍的值無效。}

任何人都知道我的代碼中存在什麼問題?

回答

0

我看你使用.GET請求,並在body中傳遞參數。所以結果會變成失敗。

+0

我應該如何在這裏添加參數?我用過這麼多次,它工作 –

+0

你用'.GET'做了嗎?據我所知,.GET只能添加標題或包含在URL中。 –

+0

關注此主題:http://stackoverflow.com/questions/978061/http-get-with-request-body,您可以添加參數'.GET'。但是這樣做沒有用處。 在你的情況下,結果將不是JSON。那爲什麼你收到失敗。 –