2017-09-01 77 views
0

使用Alamofire for .post api,api在postman中給出數據,但不在我的代碼中發佈代碼下面,請指導我在這裏做什麼錯誤:Alamofire .post api錯誤:代碼= 3840「字符1周圍的值無效

// API calling method: 

parameters = [ 
     "Address" : "" as AnyObject, 
     "Name" : "" as AnyObject, 
     "ServiceID" : "" as AnyObject, 
     "Rating" : "" as AnyObject, 
     "Price" : "" as AnyObject 
    ] 

    let headers: Dictionary = [ 
     "" : "" 
    ] 

    print(parameters) 

    ApiServices.requestPOSTURL(strURL, params: parameters, headers: headers, success:{ 

     (JSONResponse) -> Void in 

     CommonMethodsClass.hideHUD(targetView: self.view) 

     print(JSONResponse["message"]) 

     let strMsg = JSONResponse["message"].stringValue 


     if (JSONResponse["status"].intValue == 1) 
     { 

     } 
     else 
     { 
      CommonMethodsClass.showAlertMessage(vc: self, titleStr: "Error!", messageStr: strMsg) 
     } 

    }) { 
     (error) -> Void in 
     print(error) 

     CommonMethodsClass.hideHUD(targetView: self.view) 
    } 


// Api request method: 
class func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void){ 

    Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default).responseJSON { (responseObject) -> Void in 

     print(responseObject) 

     if responseObject.result.isSuccess { 
      let resJson = JSON(responseObject.result.value as Any) 
      success(resJson) 
     } 
     if responseObject.result.isFailure { 
      let error : Error = responseObject.result.error! 
      failure(error) 
     } 
    } 
} 

Error: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 1.}))

更新:響應解析,也可能需要改變

{ 
    "status": true, 
    "message": "", 
    "data": [ 
     { 
      "SalonID": "1", 
      "SalonName": "Affinity", 
      "SalonEmail": "[email protected]", 
      "SalonPhone": "9999888877", 
      "SalonMobile": "9999888877", 
      "SalonAddress": "C-28, Sec-58, India", 
      "Latitude": "18.5806", 
      "Longitude": "27.36273", 
      "Image": null, 
      "SalonImage": "", 
      "TimeIntervalminutes": 20, 
      "AverageRating": 4, 
      "IsActive": 1 
     }, 
     { 
      "SalonID": "3", 
      "SalonName": "Looks", 
      "SalonEmail": "[email protected]", 
      "SalonPhone": "99998828877", 
      "SalonMobile": "99998388877", 
      "SalonAddress": "GP Mall,India", 
      "Latitude": "", 
      "Longitude": "", 
      "Image": null, 
      "SalonImage": "", 
      "TimeIntervalminutes": 30, 
      "AverageRating": 5, 
      "IsActive": 1 
     } 
    ] 
} 
+0

變化'responseJSON'到'responseString' –

+0

但相同的代碼工作對於另一個API –

+0

我認爲這個API響應與該API不同。這就是爲什麼它無法獲得。 Alamofire發現接收類型有一些可疑的價值,這就是爲什麼它會出現這種錯誤。 –

回答

1

responseJSON替換爲responseString

您需要將值轉換爲字符串。你能更新你的問題中的字符串嗎?

您可以使用此功能來響應字符串轉換成JSON:

func convertToDictionary(text: String) -> [String: Any]? { 
    if let data = text.data(using: .utf8) { 
     do { 
      return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] 
     } catch { 
      print(error.localizedDescription) 
     } 
    } 
    return nil 
} 

字符串:let str = "{\"name\":\"James\"}"

用法:let dict = convertToDictionary(text: str)

+0

請現在檢查問題 –

0

嘗試responseString代替responseJSON,它會工作

Alamofire.request("URL").responseString { response in 
    print(response) 

    if let json = response.result.value { 
     print("JSON: \(json)") 
    } 
} 
+0

可以請你解析指導嗎? –

相關問題