gpt4 book ai didi

iphone - NSURLConnection 返回数据但我无法读取它

转载 作者:行者123 更新时间:2023-12-03 20:25:18 27 4
gpt4 key购买 nike

这是另一个简单的问题,我在 Google 上搜索了一个小时后却找不到答案...

用户正在文本字段中输入内容,当该字段失去焦点时,我启动 NSURLConnection....

NSString *usernameTry = [sender text];
NSLog(@"username field = %@",usernameTry);

NSString *content = [@"http://www.siebenware.de/Rhythm/user/checkusername.php?username=" stringByAppendingString:usernameTry];
content = [content stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:content] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
NSLog(@"%@",content);

然后我等待回复......

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Received Response");
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
int status = [httpResponse statusCode];

if (!((status >= 200) && (status < 300))) {
NSLog(@"Connection failed with status %@", status);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[resultsData appendData:data];
NSString *resultsString = [[NSString alloc] initWithData:resultsData encoding:NSUTF8StringEncoding];
NSLog(@"received data %@ %i",resultsString,[resultsString length]);

if ([resultsString isEqualToString: @"FAIL"]){
NSLog(@"failed to retrieve data");
}else{
resultsObjects = [[resultsString componentsSeparatedByString:@":"] mutableCopy];

if([resultsObjects objectAtIndex:0]==@"usernamecheck"){
if ([resultsObjects objectAtIndex:1]==username.text) {
if ([resultsObjects objectAtIndex:2]==@"NG"){
//set the check marker to bad
[usernameVer setImage:[UIImage imageNamed:@"redcross.png"]];
}else{
//set the check market to good
[usernameVer setImage:[UIImage imageNamed:@"greentick.png"]];
}
}

}
}
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Unable to retrieve data" message:[NSString stringWithFormat:@"Error code %i",[error code]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

}

我看到响应到达 didReceiveResponse 并收到 18 字节的数据(这是正确的)。

但是 didReceiveData 说“数据”为空。我知道我错过了一些非常简单的事情,我保证一旦解决了这个问题我就会踢自己。

问候克里斯·H

最佳答案

如果你想将数据更改为 NSString,你可以这样做:

NSString *responsestring = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", responsestring);

关于iphone - NSURLConnection 返回数据但我无法读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10190090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com