我使用POST方法获取数据。我已成功检索到所有数据。在UI中显示它需要很长时间,但我可以立即在控制台上打印,我的代码是
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.xxxyyy.com/v1/api/client/authorize"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"ABCD" forHTTPHeaderField:@"Authkey"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Authkey"];
NSData* data1 = [requestReply dataUsingEncoding:NSUTF8StringEncoding];
jsonReturnArray = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingAllowFragments error:&error];
NSArray *array = [jsonReturnArray copy];
[self rec:array];
NSString *phoneNumber=[NSString stringWithFormat:@"%@",[jsonReturnArray valueForKey:@"phone"]];
lblPhoneNumber.text = phoneNumber;
NSString *Address=[NSString stringWithFormat:@"%@ %@ %@,CA %@",[jsonReturnArray valueForKey:@"street1"],[jsonReturnArray valueForKey:@"street2"],[jsonReturnArray valueForKey:@"city"],[jsonReturnArray valueForKey:@"postalcode"]];
lblAddress.text=Address;//takes long time to display
NSLog(@"%@",Address);//immeaditely print
strlatitude=[jsonReturnArray valueForKey:@"latitude"];
strlongitude=[jsonReturnArray valueForKey:@"longitude"];
[self Map:(MKMapView *)mapLocation didUpdateUserLocation:(MKUserLocation *)nil];//method call
}] resume];
答案 0 :(得分:0)
这需要花费太多时间来打印数据,但是如果你使用NSURLConnection类,它可能对你有帮助。这是我的Class方法,它可能会有所帮助。
+ (void)postRequestData:(NSDictionary *)postVars
Action:(APIMode)action
WithCompletionHandlar:(void (^) (id result, BOOL status))completionBlock
{
NSURL *url = [NSURL URLWithString:API_URL([self getAPINameForType:action])];
NSLog(@"Request URL %@",[NSString stringWithFormat:@"%@",url]);
NSString *contentType = @"application/json";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSError *err = nil;
NSMutableDictionary *params=[[NSMutableDictionary alloc] initWithDictionary:postVars];
// [params setObject:[self getAPINameForType:action] forKey:@"mode"];
NSLog(@"Paramater %@",params);
NSData *body = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&err];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)body.length] forHTTPHeaderField: @"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if(!connectionError)
{
NSError *error = nil;
NSDictionary *dictResponse = [NSDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments error:&error]];
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(dictResponse,(error == nil));
});
NSLog(@"%@",dictResponse);
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(connectionError.localizedDescription,NO);
});
}
}];
}
使用此方法代替它。由于NSURLConnection类在后台执行,因此执行速度很快。
答案 1 :(得分:0)
尝试使用NSURLConnection类(手动代码)获取数据,或者只使用AFNetworking类(代码较少)。 AFNetworking内部使用NSURLConnection类本身。