我使用 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];