解析:JSON文本不是以数组或对象开头,而是选项允许未设置片段

时间:2016-08-20 09:02:02

标签: ios parse-platform

我有一个使用Parse的系统,它有一个Web前端和一个iOS和Android客户端。 在我们转移到 HTTPS

之前,一切正常

在Android和Web(Javascript)上一切正常但是当我尝试在 iOS

上传文件时收到以下消息

JSON文本没有以数组或对象开头,也没有允许未设置片段的选项。

导致它的代码是。如果我在上传中包含图片(Search.sharedInstance.imageURL!= nil),它只会失败

// Send to Parse             
if PFUser.currentUser() != nil {
    sentReport["user"] = Search.sharedInstance.currentUser
}

sentReport["reportType"] = userSelectedReportType

Search.sharedInstance.reportText = reportNotesTextView.text
sentReport["reportDescription"] = Search.sharedInstance.reportText

if (reportLatCoords != nil && reportLngCoords != nil) {
    sentReport["reportPosition"] = PFGeoPoint(latitude: reportLatCoords!, longitude: reportLngCoords!)
}

sentReport["search"] = Search.sharedInstance.selectedPerson

let reportImageFile = PFFile(name: "\(Search.sharedInstance.timeStamp).jpg", data: reportImage!)

if reportPhoto.image != UIImage(named: "selectPhoto") {
    if Search.sharedInstance.imageURL != nil {
        sentReport["reportPicture"] = reportImageFile
        sentReport.setObject(Search.sharedInstance.imageURL!.absoluteString, forKey: "reportImageUri")
    }
}

sentReport["loggedAt"] = date

// Handle success & failure
sentReport.saveInBackgroundWithBlock { (success, error) -> Void in

    if success == true {
        self.dismissViewControllerAnimated(true, completion: { () -> Void in })
    } else {

        if Reachability.isConnectedToNetwork() == false {
            sentReport.pinInBackgroundWithBlock { (success, error) -> Void in
                if success {
                    Search.sharedInstance.syncReportItemsWithServer()
                }
            }
            self.displayAlert("No internet connection available", message: "But don't worry, your report will be sent automatically when you regain an internet connection.")
        }
    }
}    

我正在努力的一点是,我在很多时候阅读/解析了这个问题但是我无法控制图片上传。它在Android SDK中运行良好吗?

我的服务器地址是:https://lowlands.lab-cloud.net/parse

更新

ReportImage

 var reportImage = UIImageJPEGRepresentation(self.reportPhoto.image!,
    1.0)

if reportImage!.length < 10000000 {
    reportImage = UIImageJPEGRepresentation(self.reportPhoto.image!, 0.75)
} else if (reportImage!.length >= 10000000) && (reportImage!.length <= 20000000) {
    reportImage = UIImageJPEGRepresentation(self.reportPhoto.image!, 0.50)
} else if (reportImage!.length > 20000000) {
    reportImage = UIImageJPEGRepresentation(self.reportPhoto.image!, 0.25)
}

1 个答案:

答案 0 :(得分:2)

问题出在你的reportImage变量上,遗憾的是你并没有与我们分享......这就是你在PFFIle中存储UIImages的方法

let imageData = UIImagePNGRepresentation(image)
let imageFile = PFFile(name:"image.png", data:imageData)

var userPhoto = PFObject(className:"UserPhoto")
userPhoto["imageName"] = "My trip to Hawaii!"
userPhoto["imageFile"] = imageFile
userPhoto.saveInBackground()