Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)

Paste

Pasted as Swift by registered user mesutde ( 9 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: