1

I am using below code to get json data from server and it is returning error.

code :

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)
            }


    }
}

Here is the error message: 0 Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}

Any one know what is the issue in my code?

Sandeep Agrawal
  • 496
  • 6
  • 21

1 Answers1

0

I see you use .GET request, and transfer parameter in body. So the result will become Failure.

Tho Nguyen
  • 125
  • 11
  • How should I add the parameter here? I used like this many time and it worked – Sandeep Agrawal Aug 05 '16 at 08:32
  • you did it with `.GET`? As i know, .GET only can add in header or include on URL. – Tho Nguyen Aug 05 '16 at 08:43
  • Following this topic: http://stackoverflow.com/questions/978061/http-get-with-request-body , you can add parameter `.GET`. But it's never useful to do. In your case the result will be not JSON. that why you receive a Failure. – Tho Nguyen Aug 05 '16 at 08:51