Tuesday, 10 July 2018

Request time out error 1001 using alamofire in ios swift?

Here i am sending parameter value like username, user toke, post id, etc to backend using alamofire. if status success then, notification will will send from backend. Inside postnotification function i have tried post method code using alamofire and datatask method but it does not work. In console i am getting request time out or nothing.
Here is my code :
   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)
  //        }



  }

Any help much appreciated pls..


from Request time out error 1001 using alamofire in ios swift?

No comments:

Post a Comment