1

I'm sure this is stupid simple for all you seasoned guys out there, but for beginners it is tough!

I'm basically populating a table with a url that displays a json list.

So I have this in on top:

super.viewDidLoad()
get_data_from_url("http://www.example.com/my_jsonfile.php")

The following code works fine with a simple url without parameters:

  func get_data_from_url(url:String)
{
    let httpMethod = "GET"
    let timeout = 15
    let url = NSURL(string: url)
    let urlRequest = NSMutableURLRequest(URL: url!,
        cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
        timeoutInterval: 15.0)
    let queue = NSOperationQueue()
    NSURLConnection.sendAsynchronousRequest(
        urlRequest,
        queue: queue,
        completionHandler: {(response: NSURLResponse!,
            data: NSData!,
            error: NSError!) in
            if data.length > 0 && error == nil{
                let json = NSString(data: data, encoding: NSASCIIStringEncoding)
                self.extract_json(json!)
            }else if data.length == 0 && error == nil{
                println("Nothing was downloaded")
            } else if error != nil{
                println("Error happened = \(error)")
            }
        }
    )
}

However how can I get my parameters in there? i.e.: the url should be:

http://www.example.com/my_jsonfile.php?the_date=2015-05-16&leaving=yes
2
  • why can't you include the parameters in your call to get_data_from_url? Commented Apr 26, 2015 at 18:37
  • I tried adding it straight to get_data_from_url all in one line but it doesn't work. It doesn't crash or anything, but it just doesn't seem to pass along the get variables Commented Apr 26, 2015 at 20:14

1 Answer 1

1

You have to concatenate the URL String, so that the parameters you want are in it:

var url = "http://www.example.com/my_jsonFile.php?"

var parameters = ["the_date=2015-05-16", "leaving=yes"]

for parameter in parameters{
  url+=parameter
  if parameter != parameters.last{
     url+="&"
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Please show me how if you don't mind. I added all your code before my get_data_from_url function, and then changed my function to get_data_from_url(url) but that didn't help
What do you mean by "it didnt help"? the String you want to use as URL is in the url variable. Configure the parametersin the array and pass url to you function. If you still hve problems, please post the changed code
You were right. I had a typo in there. Thanks much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.