Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as Swift by registered user mesutde ( 7 years ago )
@IBAction func request(_ sender: Any) {
        
        let requestURL: NSURL = NSURL(string: "https://glosbe.com/gapi/translate?from=eng&dest=tr&format=json&phrase=cat&pretty=true")!
        let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
        let session = URLSession.shared
        
        /*
         Request Esnasında bilgi gönderilecek ise POST medthodu ile request işlemi örneği
         
         let reqJson = ["User":"IOSClient"]
         let reqData : NSData = NSKeyedArchiver.archivedData(withRootObject: reqJson) as NSData
         JSONSerialization.isValidJSONObject(reqJson)
         
         
         urlRequest.httpMethod = "POST"
         urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
         urlRequest.httpBody = reqData as Data
         
         */
        let task = session.dataTask(with: urlRequest as URLRequest) {
            (data, response, error) -> Void in
            
            let httpResponse = response as! HTTPURLResponse
            let statusCode = httpResponse.statusCode
            //print("jsonParse")
            if (statusCode == 200) {
                print("Everyone is fine, successfully.")
                
                do{
                    
                    let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:AnyObject]
                    
                    //print(json["stations"] ?? "asda")
                    let isSuccess : String = json["result"] as! String
                    
                    if isSuccess == "ok"
                    {
                        print("ok")
                    }
                    else
                    {
                        print("hata")
                    }
                    /*
                     if let stations = json["stations"] as? [[String: AnyObject]] {
                     
                     for station in stations {
                     
                     if let stationName = station["stationName"] as? String {
                     
                     //print(stationName)
                     
                     
                     }
                     }
                     }
                     */
                    //
                }catch {
                    print("Error with Json: \(error)")
                }
            }
        }
        
        task.resume()
    }

 

Revise this Paste

Children: 82721
Your Name: Code Language: