V
V
Vladislav Resley2020-05-24 01:09:35
iOS
Vladislav Resley, 2020-05-24 01:09:35

I am using weatherapi.com init URL(string: urlString). The received link works in the browser, but the application does not receive data from it. What's the matter?

I am using weatherapi.com init URL(string: urlString). The received link works in the browser, but the application does not receive data from it. Code below

func ferchFromServer(cityName: String) {
        let urlString = "https://api.weatherapi.com/v1/current.json?key=\(apikey!)&q=\(cityName)"
        let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
        print(url!)
        let session = URLSession(configuration: .default)
        let task = session.dataTask(with: url!) { (data, response, error) in
            if let receivedData = data {
                if let datas = self.parseJSON(data: receivedData){
                    self.delegate?.updateInterface(self, datas: datas)
                }
                
            }
        }
        task.resume()
    }


print(url) > https://api.weatherapi.com/v1/current.json?key=<MYKEY>&q=New%20York

This is a working link and if you paste it into the browser, the desired answer comes (The key is hidden)

But the application writes this to the console>
The data couldn’t be read because it isn’t in the correct format.


PS If I use a city of one word like Moscow or Istanbul, then everything works correctly and the application receives data, such a problem with cities of two or more words

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mstrepetov, 2020-05-28
@mstrepetov

Try not to use a space (%20) as a delimiter in the cityName variable. And use en dash
https://api.weatherapi.com/v1/current.json?key=&q=... returns correct data
https://api.weatherapi.com/v1/current.json?key=&q= ... also returns correct data, but it's better not to use + in the address bar)))
Where do you study?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question