gpt4 book ai didi

ios - 如何解决 NSURLError : The network connection was lost?

转载 作者:行者123 更新时间:2023-11-29 01:26:59 30 4
gpt4 key购买 nike

我正在开发一个应用程序,我在其中使用网络服务方法从服务器获取数据。

下面是我的代码。

responseData = [NSMutableData data];

NSString *service = @"/getActivity.php";

NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"URL"];
urlLoc= [urlLoc stringByAppendingString:service];

loginUser = [[NSUserDefaults standardUserDefaults]valueForKey:@"username"];
NSLog(@"%@",loginUser);

NSLog(@"%@",selectUsername);
// this is correct
//NSString *requestStringg = [NSString stringWithFormat:@"{\"?username\"=\"%@\"&\"follower_username\"=\"%@\"}",loginUser,selectUsername];
NSDate * date = [NSDate date];
// NSLog(@"Date:%@",date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *date_str = [dateFormat stringFromDate:date];

NSString *requestString = [NSString stringWithFormat:@"username=%@&follower_username=%@&follower_created_date=%@",loginUser,selectUsername,date_str];

NSData *postData = [requestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:urlLoc]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

postFollowUnfollowConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


- (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(@"error :%@",error);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

if (connection == postNotificationConnection)
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);

}

}

我的网络服务工作正常,但在第 4 次或第 5 次调用网络服务后,它进入了 didFailWithError,向我显示以下错误。

enter image description here

我已经为这个错误尝试了所有可能的解决方案,但没有任何帮助。此错误在 iOS 8 和 iOS 9 中产生。

如有任何帮助,我们将不胜感激。

最佳答案

1) 这是 iOS 8 的问题,但可以使用

[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

代替

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

2) 其他解决方案是增加 RequestTime:

[request setTimeOut:180];

关于ios - 如何解决 NSURLError : The network connection was lost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887043/

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