1

我使用 STHTTPRequest 在 IOS 应用程序中获取 JSON 数据。请看下面的代码。当我在 Iphone 6 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];
4

2 回答 2

0

我想我找到了解决方案。基本上 IOS 从第一个响应中获取“保持活动”参数,并认为连接是持久的。当进行下一个 JSON 调用时,IOS 正在尝试使用已过期的现有连接。我添加了 header('Connection: close'); 在每个 PHP Web 服务中。我在每个 Web 服务中告诉 ios 连接已关闭。我不确定这是否是一个好方法。我已经在设备上测试了 20 分钟。它的工作。我很感激对此的任何想法。

于 2015-06-26T17:57:52.227 回答
0

在 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);

它奏效了!

于 2017-02-16T15:59:22.230 回答