0

I want to get data from the website mentioned in the code but I have an error and I didn't khow what I should do to solve it. Is there any one who can help me? Thank you

these is the code that I used

 @IBAction func onGetTapped(_ sender: Any) {
   guard let url = URL(string: "http://gpsforanimal.herokuapp.com/") else { return }
 let session = URLSession.shared
 session.dataTask(with: url) { (data, response, error) in
                if let response = response {
                    print(response)
                }
                if let data = data {
                    print(data)
                    do {
                        let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]
                        print(json)
                    } catch {
                        print(error)
                    }

                }
            }.resume()
        }

and I get this

<NSHTTPURLResponse: 0x16d47ac0> { URL: http://gpsforanimal.herokuapp.com/ } { status code: 200, headers {
    "Cache-Control" = "max-age=0, private, must-revalidate";
    Connection = "keep-alive";
    "Content-Length" = 17536;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Thu, 05 Jul 2018 21:57:30 GMT";
    Etag = "W/\"cde6bf7e5688b2395a936b5785c715c4\"";
    Server = "WEBrick/1.3.1 (Ruby/2.2.6/2016-11-15)";
    "Set-Cookie" = "_GPS_session=MkZTb1NWQmp1QnA5MXFpUHQ3Wk1NdHV6Vm5ESjNGR1M5cC9jMUkxRTFVQXFuU1VTWko0R2F2a29WV3hNMzczcTAzVjB2TmpBTG5QbGFwcUVObnFHTEp5T0J6NWlFNXNBZ1ZJT1M4R3ZxRWthTzV4RUswalk3eDlrME1mc09KRGZlYzhLaGwxSlh1aGVXcDlPaFF4Vi9RPT0tLUNzNHpzUlVHSXFkZ3czZThyZU9ZMlE9PQ%3D%3D--c9fbcde23b38905f956d4a139629c710565d1015; path=/; HttpOnly";
    Via = "1.1 vegur";
    "X-Content-Type-Options" = nosniff;
    "X-Frame-Options" = SAMEORIGIN;
    "X-Request-Id" = "c2c9432e-37dd-4f44-9d10-62314f2c8ebc";
    "X-Runtime" = "0.040508";
    "X-Xss-Protection" = "1; mode=block";
} }
17536 bytes
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x16d63c60 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
Lina
  • 3
  • 3
  • 1
    `JSON text did not start with array or object and option to allow fragments not set.` – Robert Harvey Jul 05 '18 at 22:19
  • http://gpsforanimal.herokuapp.com/ the response from this url is not a JSON – Osman Jul 05 '18 at 22:22
  • Possible duplicate of [How to parse JSON response from Alamofire API in Swift?](https://stackoverflow.com/questions/26114831/how-to-parse-json-response-from-alamofire-api-in-swift) – Tanvir Nayem Jul 06 '18 at 02:35

4 Answers4

2

try this :

func getURL(){
    var request = URLRequest(url: URL(string: "http://gpsforanimal.herokuapp.com/positions.json")!)
    request.httpMethod = "GET"
    let session = URLSession.shared
    session.dataTask(with: request) {data, response, error in
        if error != nil {
            print(error!.localizedDescription)
        }
        guard let data = data else { return }
        do {
            //Decode retrived data with JSONDecoder and assing type of Article object
            let realData = try JSONDecoder().decode([gps].self, from: data)
            print(realData)
        } catch let jsonError {
            print(jsonError)
        }
        }.resume()
}

and create a struct with codable like this :

struct gps : Codable{
    var id = Int()
    var longitude  : String?
    var latitude : String?
    var created_at = String()
    var updated_at = String()
    var url = String()

    init(){
        self.id = Int()
        self.longitude = String()
        self.latitude  = String()
        self.created_at = String()
        self.updated_at = String()
        self.url = String()
    }
}

and then call your function getURL()

Osman
  • 1,496
  • 18
  • 22
  • thank you,That's work good, can you tell me how can I get the last data (latitude and longitude) – Lina Jul 05 '18 at 23:50
  • ok great, don't forget to rate the answer. The realData is now a array from your response. You can do whatever you want for example just iterate the array in a loop like this : for i in articlesData{ print(i.latitude) print(i.longitude) } – Osman Jul 05 '18 at 23:53
  • for your last item do this :if let data = articlesData.last { print(data.latitude) print(data.longitude) } – Osman Jul 05 '18 at 23:58
  • Do you have an idea of how to use mapView to see the location when I inter the latitude and longitude? – Lina Jul 06 '18 at 03:11
2

For swift4.1 don't need to initialise

func getURL(){
    var request = URLRequest(url: URL(string: "http://gpsforanimal.herokuapp.com/positions.json")!)
    request.httpMethod = "GET"
    let session = URLSession.shared
    session.dataTask(with: request) {data, response, error in
        if error != nil {
            print(error!.localizedDescription)
        }
        guard let data = data else { return }
        do {
            //Decode retrived data with JSONDecoder and assing type of Article object
            let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            let realData = try decoder.decode([gps].self, from: data)
            print(realData)
        } catch let jsonError {
            print(jsonError)
        }
        }.resume()
}

Create a struct :

struct gps : Decodable{
    var id :Int
    var longitude  : String?
    var latitude : String?
    var createdAt : String
    var updatedAt : String
    var url : String
 }
0

Can you change the URL to http://gpsforanimal.herokuapp.com/positions.json and try again;

The URL you provided returned a table render of the JSON, you need to pull the JSON rather than the table.

Ilan P
  • 1,602
  • 1
  • 8
  • 13
  • Thank you for your reply,that's work good.Do you have an idea about how to get the last data(the last coordinate)on the website – Lina Jul 05 '18 at 23:53
-2

Try using AlamoFire to get data. You can use SwiftyJSON to analyze and better get the data from the JSON response from the website.

Sylvan_M
  • 23
  • 3