get 和post 方式请求数据

get 和post 方式请求数据的时候,只想说一句话,别忘了对NSMutableData *a 也就是用来接受数据的中间实例进行初始化........

get 异步请求网络数据的步骤

  NSURL *url = [[NSURL alloc] initWithString:@"http://f.hiphotos.baidu.com/image/w%3D2048/sign=2f72d721bb12c8fcb4f3f1cdc83b9345/ac4bd11373f082020756343e49fbfbedaa641bee.jpg"];


    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15];


    NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:request delegate:self];


    [connect release];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)respons
{
    [self.aview setProgress:0 animated:YES];
    self.a = [respons expectedContentLength];
    [self.receiverData setLength:0];
    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiverData appendData:data];
    float b = [self.receiverData length]/self.a;
    [self.aview setProgress:b animated:YES];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 
    self.imageView.image  = [UIImage imageWithData:self.receiverData];
    
}
同步

    NSURL *url = [[NSURL alloc] initWithString:@"http://f.hiphotos.baidu.com/image/w%3D2048/sign=2f72d721bb12c8fcb4f3f1cdc83b9345/ac4bd11373f082020756343e49fbfbedaa641bee.jpg"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    UIImage *image = [UIImage imageWithData:data];
    self.imageView.image = image;

你可能感兴趣的:(get 和post 方式请求数据)