Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ios中从非ui线程发送http请求_Ios_Http_Nsurlconnection_Nsurlrequest - Fatal编程技术网

如何在ios中从非ui线程发送http请求

如何在ios中从非ui线程发送http请求,ios,http,nsurlconnection,nsurlrequest,Ios,Http,Nsurlconnection,Nsurlrequest,我正在寻找一个示例来发送和接收iOS中的http GET请求。我只想 do是在后台线程中处理通信,这样它就不会阻塞主线程 并且还希望处理http标准错误代码。有人能给我推荐参考代码或密码吗 处理http响应数据和处理适当内存管理的示例 任何帮助都将是感激的。实现这一目标的两种方法: 1) NSURLCOnnection sendAsynchronousRequest方法: NSString *strURL= [NSString stringWithFormat:@"http://www.goog

我正在寻找一个示例来发送和接收iOS中的http GET请求。我只想 do是在后台线程中处理通信,这样它就不会阻塞主线程 并且还希望处理http标准错误代码。有人能给我推荐参考代码或密码吗 处理http响应数据和处理适当内存管理的示例


任何帮助都将是感激的。

实现这一目标的两种方法:

1)
NSURLCOnnection sendAsynchronousRequest
方法:

NSString *strURL= [NSString stringWithFormat:@"http://www.google.com/"];
NSURL *URL = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *requestURL = [[NSURLRequest alloc] initWithURL:URL];
[NSURLConnection sendAsynchronousRequest:requestURL
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data,        NSError *error)
 {      
     NSLog(@"Response is:%@",[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
 }];
2) 创建并激发请求,然后
NSURLConnection
委派方法以获得响应:

// Create the request.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL     URLWithString:@"http://google.com"]];

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse  *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection 
return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}

@whitewolf09的可能副本谢谢链接。接受的答案显示了一些片段,但似乎不完整。因为它没有提到这些回调将在哪个线程上执行。使用这种方法,当http请求完成时,您的主线程将收到委托方法的通知。在此过程中,主线程不会被阻塞。如果您愿意,您可以使用第三方库,如AFNetworking(),这将使您的网络相关编程更加轻松谢谢您,我正在寻找第一个答案!对于重复调用(如集合视图)中的下载最有用。请问为什么使用
stringByAddingPercentEscapesUsingEncoding
方法?