NSURLConnection-didReceiveData未';不要打电话-iOS&;弧
我目前正在试用twitter流媒体api,并尝试使用NSURLConnection-didReceiveData未';不要打电话-iOS&;弧,ios,http,ios6,nsurlconnection,Ios,Http,Ios6,Nsurlconnection,我目前正在试用twitter流媒体api,并尝试使用NSURLConnection获取流媒体。由于twitter不起作用,我简化了一切,并试图从谷歌网站上获取一些源代码,但这也不起作用 连接开始和结束,但不调用didReceiveData委托。我肯定我错过了什么。希望你们能帮助我 在标题中:@interface ViewController:UIViewController 在身体里: - (void)viewDidLoad { [super viewDidLoad]; NSU
NSURLConnection
获取流媒体。由于twitter不起作用,我简化了一切,并试图从谷歌网站上获取一些源代码,但这也不起作用
连接开始和结束,但不调用didReceiveData
委托。我肯定我错过了什么。希望你们能帮助我
在标题中:@interface ViewController:UIViewController
在身体里:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLConnection *connection;
NSMutableURLRequest *request;
// Do any additional setup after loading the view, typically from a nib.
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[request setHTTPMethod:@"GET"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL {
NSLog(@"Stream finished.");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", dataString);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Connection failed!");
}
这里有人说这是他们的SOAP格式 这也可能是你正在寻找的
您的NSURLConnection局部变量
connection
在viewDidLoad
的末尾超出范围。向ViewController添加一个属性,以在作用域中保存NSURLConnection变量 一些想法
destinationURL
参数:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s", __FUNCTION__);
}
nsurauthenticationchallengesender
的存在,如果你期待一个挑战(这是你在谷歌网站上得不到的),那么你显然会相应地处理它:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// For example, if you have a userid/password to use, see if this is the first
// challenge, and then tell NSURLConnection to try using those credentials, and if
// it failed a second time, you might just cancel the authentication challenge.
if (challenge.previousFailureCount == 0) {
NSURLCredential *credential = [NSURLCredential credentialWithUser:kUserID password:kPassword persistence:NSURLCredentialPersistenceForSession];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
} else {
[challenge.sender cancelAuthenticationChallenge:challenge];
}
}
initWithRequest
或connectionWithRequest
时,您不想调用方法。仅当您使用request:delegate:startimediately:执行一个简单的initWithRequest:delegate:startimediately:
并指示它不要startimediately
时才需要它- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s", __FUNCTION__);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%s: %@", __FUNCTION__, dataString);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%s error=%@", __FUNCTION__, error);
}
对不起,我在复制过程中遗漏了一些内容并通过了。我使用了[连接启动]。但之前我也试过你的方法。不起作用。。我将更新这个问题。我怀疑第一个问题只是因为他们的服务器代码中有一个bug,根本无法生成响应,第二个问题是他们试图从后台队列调用
NSURLConnection
(如果你不使用运行循环,这是有问题的……只需从主队列启动就更容易了)。我复制并粘贴了你的代码到一个新的应用程序中,我得到了日志“Stream Finished”。这不是必需的。它会在连接期间为您保留。有时,这很有用,但通常您不需要这样做。感谢您的回答。。问题在于ConnectiondFinishLoading。。我在某个网站上跟进了一个教程,其中使用了另一个代理。。我删除了它并添加了您的。它是(现在……)