2016-11-30 36 views
1

是我的参数POST方法不适用于swift 3.0 Alamofire 4.0?下面

var comment: [String:AnyObject] = [:] 
comment = [ 
      "txtUsername": (txtUsername?.text)! as AnyObject, 
      "txtPassword": (txtPassword?.text)! as AnyObject 
     ] 

和POST方法函数包含下面的代码

Alamofire.upload(
     multipartFormData: { multipartFormData in 

      for (key, value) in Param { 
       multipartFormData.append((value.data(using: .utf8))!, withName: key) 
      } 

      let image = UIImage(named: "bodrum.png")! 
      if let imageData = UIImageJPEGRepresentation(image, 1) 
      { 
       multipartFormData.append(imageData, withName: "file", fileName: "file.png", mimeType: "image/png") 
      } 

    }, 
     to: webpath, 
     encodingCompletion: 
     { 
      encodingResult in 

      switch encodingResult 
      { 
       case .success(let upload, _, _): 
        upload.responseJSON 
        { 

         response in 

         guard case .success(let rawJSON) = response.result else { 
          return 
         } 
         guard let json = rawJSON as? [String: String] else { 
          return 
         } 

         if json["Success"] == "True"{ 
          print("Success") 
         } 
         else{ 
          print("Failure") 
         } 
        } 

       case .failure(let encodingError): 
        print(encodingError) 
      } 
    } 
    ) 

这里的目标C我的web服务成功的工作,但在使用此代码它总是给予回应“假”?这段代码中是否有任何错误。

+0

不知道它是否解决了这个问题,但是在Swift 3中注解了'[String:Any]' - 或者省略了注释,因为字典显然是'[String:String]' - 并且将这些强制类型转换为'AnyObject' – vadian

+0

您的意思是打印“失败”吗? –

回答

0

许多事情都可能出错。有一对夫妇的事情,如果我要调试我会检查:

  1. 如果我想使用PNG,我可能会在这段代码使用UIImagePNGRepresentation(...),而不是UIImageJPEGRepresentation
  2. 如果我想使用JPEG,我会将multipartFormData.append(imageData, withName: "file", fileName: "file.png", mimeType: "image/png")更改为multipartFormData.append(imageData, withName: "file", fileName: "file.jpg", mimeType: "image/jpeg")
  3. 如果(2),尝试将UIImageJPEGRepresentation(image, 1)更改为UIImageJPEGRepresentation(image, 0.5)。它可能会失败,因为图像太大,因此应用更高的压缩率可能会发现问题。
+0

即使我没有发送图像我只发送参数,然后也不起作用 – Dharini

相关问题