相关疑难解决方法(0)

如何使用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];
}
Run Code Online (Sandbox Code Playgroud)

我的第一个问题是 - …

iphone cocoa-touch json objective-c nsurlrequest

79
推荐指数
3
解决办法
12万
查看次数

标签 统计

cocoa-touch ×1

iphone ×1

json ×1

nsurlrequest ×1

objective-c ×1