我的服务器上有带有其余APLI的json文件,我想与服务器通信,现在我用此方法连接到服务器,并在viewdidload方法中调用它,我的问题是,执行此操作的最佳方法是什么,可以我用单身人士吗?如果是,它应该是什么样子?提前致谢!
感谢答案部分中的任何代码建议。
- (void)viewDidLoad
{
[super viewDidLoad];
_mapView.showsUserLocation = YES;
_mapView.delegate = self;
[self fetchData];
}
-(void)fetchData
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:BASED_URL]];
[request setHTTPMethod:@"GET"];
[request setValue:@"/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply
length] encoding: NSASCIIStringEncoding];
CLLocationCoordinate2D location;
NSMutableArray *newAnnotations = [NSMutableArray array];
NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:GETReply
options:0
error:&error];
if (error != nil)
{
// handle the error
}
for (NSDictionary *dictionary in array)
{
location.latitude = [dictionary[@"latitude"] doubleValue];
location.longitude = [dictionary[@"longitude"] doubleValue];
// create the annotation
MyAnnotation *newAnnotation;
newAnnotation = [[MyAnnotation alloc] init];
newAnnotation.company = dictionary[@"company"];
newAnnotation.coordinate = location;
[newAnnotations addObject:newAnnotation];
}
[self.mapView addAnnotations:newAnnotations];
}
更新:基于注释,Singleton不需要执行网络功能,也不建议这样做。
我该如何改善这些代码,如果您能为我提供改善代码的解决方案,不胜感激
根据我的评论。
Singleton
用于在整个应用中创建单个实例。在进行网络活动的情况下,不建议使用Singleton,因为您可能需要一次创建多个网络连接,这将无法正常工作。您可能会选择静态函数,一些好的库希望AFNetworking
减少代码。同样,您也应该使用asynchronous
网络活动,而不是synchronous
因为它会block app activity
花费一些时间来获取数据。
要修改您的代码,您可以遵循以下代码。
创建一个ApiManager
将进行所有网络活动的类,将所有代码放入该类中。请参见下文
ApiManager.h
@interface ApiManager : NSObject
+(void)fetchCoordinates:(void (^) (id result))success failure:(void (^) (NSError *error))failure;
@end
ApiManager.m
#import "ApiManager.h"
#define BASED_URL @""
@implementation ApiManager
+(void)fetchCoordinates:(void (^) (id result))success failure:(void (^) (NSError *error))failure{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:BASED_URL]];
[request setHTTPMethod:@"GET"];
[request setValue:@"/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:nil];
NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:GETReply
options:0
error:&error];
if(!error){
success(array);
}else
failure(error);
}
@end
Usage
[ApiManager fetchCoordinates:^(id result) {
NSArray *array=(NSArray*)result;
NSMutableArray *newAnnotations = [NSMutableArray array];
for (NSDictionary *dictionary in array)
{
location.latitude = [dictionary[@"latitude"] doubleValue];
location.longitude = [dictionary[@"longitude"] doubleValue];
// create the annotation
MyAnnotation *newAnnotation;
newAnnotation = [[MyAnnotation alloc] init];
newAnnotation.company = dictionary[@"company"];
newAnnotation.coordinate = location;
[newAnnotations addObject:newAnnotation];
}
[self.mapView addAnnotations:newAnnotations];
} failure:^(NSError *error) {
}];
希望能帮助到你。
干杯。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句