在iOS Swift中使用Alamofire请求超时错误1001?

时间:2018-07-05 13:24:52

标签: ios swift xcode post alamofire

在这里,我正在使用 alamofire 将参数值(如用户名,用户令牌,帖子ID等)发送到后端。如果状态成功,则通知将从后端发送。在后通知功能内部,我尝试使用 alamofire datatask方法 发布方法代码,但是它不起作用。在控制台中,我收到请求超时或什么都没有。

这是我的代码:

   func postNotification(postItem: String, post: Post) {


 //                declare parameter as a dictionary which contains string as key and value combination. considering inputs are valid



    print("Get token from post:::",post.token)
    print(postItem)
    let token = UserDefaults.standard.string(forKey: "token")
            //create the url with URL


    var parameters       = [String:Any]()

    parameters["count"]  = post.likeCount!
    parameters["likedby"]  = currentName
    parameters["postId"] = postItem
    parameters["token"] = post.token!

    let Url = String(format: "http://highavenue.co:9000/likesnotification")
    guard let serviceUrl = URL(string: Url) else { return }
    //        let loginParams = String(format: LOGIN_PARAMETERS1, "test", "Hi World")
    let parameterDictionary = ["username" : "Test", "password" : "123456"]
    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "POST"
    request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
    guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
        return
    }
    request.httpBody = httpBody

    let session = URLSession.shared
    session.dataTask(with: request) { (data, response, error) in
        if let response = response {
            print(response)
        }
        if let data = data {
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                print(json)
            }catch {
                print(error)
            }
        }
        }.resume()


  //        let headers: HTTPHeaders = ["Content-Type" :"application/x-www-form-urlencoded"]
  //
  //        Alamofire.request("http://highavenue.co:9000/likesnotification", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
 //                    //             original URL request
 //                        print("Request is :",response.request!)
 //
 //                        // HTTP URL response --> header and status code
 //                        print("Response received is :",response.response)
 //
 //                        // server data : example 267 bytes
   //                        print("Response data is :",response.data)
  //
    //                        // result of response serialization : SUCCESS / FAILURE
   //                        print("Response result is :",response.result)
   //
   //                        debugPrint("Debug Print :", response)
  //
   //
   //        }


  //        Alamofire.request("http://highavenue.co:9000/likesnotification", method: HTTPMethod.post, parameters: json, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
   //
    //            // original URL request
   //            print("Request is :",response.request!)
   //
  //            // HTTP URL response --> header and status code
    //            print("Response received is :",response.response)
   //
  //            // server data : example 267 bytes
   //            print("Response data is :",response.data)
   //
   //            // result of response serialization : SUCCESS / FAILURE
  //            print("Response result is :",response.result)
  //
  //            debugPrint("Debug Print :", response)
  //        }



  }

任何帮助,不胜感激。

1 个答案:

答案 0 :(得分:2)

是的,解决了。有一个过失错误。我在网址中使用了另一个斜杠。我将Web API更改为另一个文件夹,并在iOS代码中更改它时犯了此错误。并在此处设置超时间隔。

//now you can print the $approver_name
Hello {{ $approver_name }}

希望这个对您有帮助。