AFNetworking 3.0 GET 请求在 iOS 中保存到类中

Posted

技术标签:

【中文标题】AFNetworking 3.0 GET 请求在 iOS 中保存到类中【英文标题】:AFNetworking 3.0 GET request save into class in iOS 【发布时间】:2016-05-30 19:59:25 【问题描述】:

在网上搜索了如何使用 AFNetworking 3.0 将响应对象保存在自定义类中后,我发现的只是 AFNetworking 库与@的基本用法987654323@GET request using AFNetworking and saving response、AFNetworking send array in JSON parameters of GET request等请求。

代码:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

listOfEventsObjects = [@[] mutableCopy];
self.tableView.delegate = self;


AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager GET:@"http://api.com.getevents.php" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) 


    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSArray *h  = [responseObject objectForKey:@"events"];

    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:h options:0 error:&error];
    NSArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    for (int i  = 0 ; i < [json count]; i++) 
        geeksEvent *h1 = [geeksEvent new];

        //h1.eventId = [];
        //h1.eventId = [[json valueForKey:@"eventId"] intValue];
        //NSLog(@"json data is: %@",h1);

    

    //  events = [responseObject objectForKey:@"events"];

    if ([responseObject isKindOfClass:[NSArray class]]) 
      //  NSArray *responseArray = responseObject;
        NSLog(@"array");
        /* do something with responseArray */
     else if ([responseObject isKindOfClass:[NSDictionary class]]) 
        NSLog(@"dictionary");
        NSDictionary *responseDict = responseObject;

      //  NSDictionary *responseDict = responseObject;
        /* do something with responseDict */
    


//        AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
//
//        responseSerializer = [AFJSONResponseSerializer serializer];
//        responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json; charset=UTF-8", nil];
//    
//       // NSArray *data  = (responseObject *) NSArray;
//        NSError *error;
//        NSData *data   = (NSData *) responseObject;
//       // NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:errno error:&error];
//    
//        
//        NSLog(@"JSon arrays is: %@",data);

        //NSDictionary *jsonDict = (NSDictionary *) responseObject;

//        
//        NSLog(@"%lu",(unsigned long)events.count);
//        
//        for (int i =  0; i  <  [events count]; i ++) 
//            
//            geeksEvent *event  = [events objectAtIndex:i];
//        
//            
//            NSLog(@"event %d is %@",i,event);
//            
//            
//            @try 
//                
//                event.eventId = [[events valueForKey:@"eventId"]intValue];
//                
//                event.eventTitle  = [[events valueForKey:@"eventTitle"] componentsJoinedByString:@""];
//                
//                event.eventShortDiscription = [[event valueForKey:@"eventShortDesc"] componentsJoinedByString:@""];
//                event.eventDescription = [[event valueForKey:@"eventDescription"] componentsJoinedByString:@""];
//                
//                event.eventDate = [events valueForKey:@"eventDate"];
//                event.eventTime = [events valueForKey:@"eventTime"];
//                event.eventUrl = [events valueForKey:@"eventUrl"];
//                
//                [listOfEventsObjects addObject:event];
//                
//             @catch (NSException *exception) 
//                
//                NSLog(@"%@",exception);
//                
//             @finally 
//                
//            
//            
//        


        [self.tableView reloadData];

我不知道如何将响应对象保存到自定义类中。

geeksEvent ## 自定义类:

.h

#import <Foundation/Foundation.h>

@interface GeeksLocations : NSObject
@property (nonatomic) int geeksBranchId;
@property(nonatomic) double *geeksLongtitude;
@property (nonatomic) double *geeksLatitude;
@property (nonatomic,strong) NSString *geeksAddress;
@property (nonatomic,strong) NSString *geeksMobile;
@property (nonatomic,strong) NSString *geeksTel;
@property (nonatomic,strong) NSString *geeksOpenDays;
@property (nonatomic,strong) NSDate *geeksOpenTime;

@end

.m

#import "GeeksLocations.h"

@implementation GeeksLocations
@synthesize geeksBranchId;
@synthesize geeksLongtitude;
@synthesize geeksLatitude;
@synthesize geeksMobile;
@synthesize geeksTel;
@synthesize geeksAddress;
@synthesize geeksOpenDays;
@synthesize geeksOpenTime;
@end

响应

"success":1,"events":["eventId":"1","eventTitle":"Open Wings Tuesday","eventShortDesc":"我们全天提供开放式翅膀"," eventDiscription":null,"eventDate":"2016-05-22","eventTime":"12:49:00","eventUrl":"http://www.code-bee.net/geeks/images/ cover-7.jpg","eventId":"2","eventTitle":"Testing","eventShortDesc":"Testing","eventDiscription":null,"eventDate":"2016-05-22" ,"eventTime":"12:49:00","eventUrl":"http://www.code-bee.net/geeks/images/cover-8.jpg"]

【问题讨论】:

你在正确的轨道上。改变这个-> geeksEvent *h1 = [geeksEvent new];进入 geeksEvent *h1 = [geeksEvent alloc] init];或者你的初始化是什么来创建你的对象的新实例。根据需要设置数据。 【参考方案1】:

将所有事件添加到一个数组中,然后您将拥有一个字典数组。还要创建一个新数组来保存您的新实例。 现在:

for (NSDictionary *dictionary in myArrayContainingAllEvents)
    GeeksLocation *location = [GeeksLocation alloc] initWithDictionary:dictionary]; 
    [newArrayForInstances addObject: location];

在 GeeksLocation 类中添加以下初始化器:

-(instanceType)initWithDictionary: (NSDictionary *) dictionary 
     self = [super init];
     if(self)
            self.geeksAddress = [dictionary objectForKey:address];
            self.....         = [dictionary objectForKey:.......];
     


     return self;

希望对你有帮助

【讨论】:

以上是关于AFNetworking 3.0 GET 请求在 iOS 中保存到类中的主要内容,如果未能解决你的问题,请参考以下文章

AFNetworking 3.0 发送请求正文内容 [重复]

在 afnetworking 3.0 中发布请求?

如何在 AfNetworking 3.0 中向请求添加标头?

AFNetworking GET 3.0 崩溃

我们如何使用 AFNetworking 3.0 记录请求

基于AFNetworking 3.0的集约性网络请求API再封装