我使用STHTTPRequest在IOS应用程序中获取JSON数据。请看下面的代码。当我在iPhone6 IOS8.0上测试时,有时代码会进入错误块。这并不总是发生,只是偶尔会发生。有人能帮上忙吗?谢谢你
if(showActivity)
{
[self ShowActivityIndicatorWithTitle:@"Loading..."];
}
STHTTPRequest *request1 = [STHTTPRequest requestWithURLString:[NSString stringWithFormat:@"%@%@",kBaseUrl,api]];
[request1 setTimeoutSeconds:120.0f];
[request1 setHeaderWithName:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[request1 setHTTPMethod:@"POST"];
request1.rawPOSTData = [postData dataUsingEncoding:NSUTF8StringEncoding];
request1.completionDataBlock = ^(NSDictionary *headers, NSData* data)
{
[self HideActivityIndicator];
if(handler != nil)
{
NSError* connectionError;
handler(JSONObjectFromData(data),connectionError);
}
};
request1.errorBlock=^(NSError *error)
{
NSLog(@"Error: %@", [error localizedDescription]);
if(error != nil)
{
[[[UIAlertView alloc] initWithTitle:@"Connection Error !" message:kAlertInternetConnection delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
[self HideActivityIndicator];
}
};
[request1 startAsynchronous];发布于 2015-06-27 01:57:52
我想我找到解决方案了。基本上,IOS从第一个响应中获取"Keep Alive“参数,并认为连接是持久的。当进行下一次JSON调用时,IOS将尝试使用已过期的现有连接。我已经在每个PHP web服务中添加了头文件(‘Connection: close');我正在通知每个web服务中的ios连接已关闭。我不确定这是不是一个好的方法。我已经在设备上测试了20分钟。它起作用了。我很感谢你对此的任何想法。
发布于 2017-02-16 23:59:22
在PHP中,我添加了:
ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // just to be safe
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: ' . filesize($file));
readfile($file);它成功了!
https://stackoverflow.com/questions/31029399
复制相似问题