Questions tagged «nsurlrequest»


7
带参数的Swift GET请求
我是新手,所以我的代码可能会出现很多错误,但是我要实现的目标是将GET请求发送到带有参数的localhost服务器。鉴于我的函数有两个参数,我试图做到更多baseURL:string,params:NSDictionary。我不确定如何将两者结合到实际的URLRequest中?到目前为止,这是我尝试过的 func sendRequest(url:String,params:NSDictionary){ let urls: NSURL! = NSURL(string:url) var request = NSMutableURLRequest(URL:urls) request.HTTPMethod = "GET" var data:NSData! = NSKeyedArchiver.archivedDataWithRootObject(params) request.HTTPBody = data println(request) var session = NSURLSession.sharedSession() var task = session.dataTaskWithRequest(request, completionHandler:loadedData) task.resume() } } func loadedData(data:NSData!,response:NSURLResponse!,err:NSError!){ if(err != nil){ println(err?.description) }else{ var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: …
81 ios  swift  get  nsurlrequest 

8
如何使用NSURLRequest在Http请求中发送json数据
我是Objective-c的新手,从最近开始,我就在请求/响应中投入了大量精力。我有一个工作示例,可以调用url(通过http GET)并解析返回的json。 下面的工作示例 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [responseData setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [connection release]; //do something with the json that comes back ... (the …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.