如何使用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 fun part)}- (void)viewDidLoad{
[self searchForStuff:@"iPhone"];}-(void)searchForStuff:(NSString *)text{
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];}我的第一个问题是 - 这种方法会扩大吗?或者这不是异步(意味着我在应用程序等待响应时阻止UI线程)我的第二个问题是 - 如何修改此请求部分以执行POST而不是GET?它只是简单地修改HttpMethod吗?[request setHTTPMethod:@"POST"];最后 - 如何将一组json数据作为简单字符串添加到此帖子中(例如){
"magic":{
"real":true
},
"options":{
"happy":true,
"joy":true,
"joy2":true
},
"key":"123"}先感谢您
3 回答
- 3 回答
- 0 关注
- 1225 浏览
添加回答
举报
0/150
提交
取消