二、社区
1. 获取数据:通过Charles截取当TabBar切换到“社区”时,发送GET请求获取数据
<1> 获取推广数据:浏览器中输入网址:http://s.budejie.com/op2/promotion/bsbdjhd-iphone-5.0.9-appstore/0-100.json 得到JSON数据。截取其中一部分

经过分析,只需要 url 和 image 创建模型类
@interface GYPromotionItem : NSObject
@property (strong, nonatomic) NSString* url;
@property (strong, nonatomic) NSString* image;
@end
添加模型数组
@interface GYNewViewController ()
@property (strong, nonatomic) NSArray<GYPromotionItem*> *promotions;
@end
用AFN获取数据,并且解析得到GYPromotionItem模型数组:
- (void)loadPromotionData {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlStr = @"http://s.budejie.com/op2/promotion/bsbdjhd-iphone-5.0.9-appstore/0-100.json";
[manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary* _Nullable responseObject) {
NSArray *array = responseObject[@"result"][@"faxian"][@"1"];
self.promotions = [GYPromotionItem mj_objectArrayWithKeyValuesArray:array];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
<2> 获取订阅数据:浏览器中输入网址:http://d.api.budejie.com/forum/subscribe/bsbdjhd-iphone-5.0.9.json 得到 JSON数据。

这里可以看到这是一个模型数组。
@interface GYSubscribeItem : NSObject
@property (strong, nonatomic) NSString* theme_name;
@property (strong, nonatomic) NSString* image_detail;
@property (strong, nonatomic) NSString* info;
@property (strong, nonatomic) NSString* image_list;
@end
添加模型数组
@interface GYNewViewController ()
@property (strong, nonatomic) NSArray<GYPromotionItem*> *promotions;
@property (strong, nonatomic) NSArray<GYSubscribeItem*> *subscribes;
@end
解析数据
- (void)loadSubscribeData {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlStr = @"http://d.api.budejie.com/forum/subscribe/bsbdjhd-iphone-5.0.9.json";
[manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary* _Nullable responseObject) {
NSArray *array = responseObject[@"list"];
self.subscribes = [GYSubscribeItem mj_objectArrayWithKeyValuesArray:array];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
<3> 获取热门搜索数据:浏览器中输入网址:http://d.api.budejie.com/topic/hotsearch/bsbdjhd-iphone-5.0.9/0-20.json 得到 JSON数据。

这是一个模型数组。
@interface GYHotSearchItem : NSObject
@property (assign, nonatomic) NSInteger count;
@property (strong, nonatomic) NSString* name;
@end
添加模型数组
@property (strong, nonatomic) NSArray<GYPromotionItem*> *promotions;
@property (strong, nonatomic) NSArray<GYSubscribeItem*> *subscribes;
@property (strong, nonatomic) NSArray<GYHotSearchItem*> *hotSearches;
解析数据
- (void)loadHotSearchData {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlStr = @"http://d.api.budejie.com/topic/hotsearch/bsbdjhd-iphone-5.0.9/0-20.json";
[manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary* _Nullable responseObject) {
self.hotSearches = [GYHotSearchItem mj_objectArrayWithKeyValuesArray:responseObject[@"hot_search"]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
- 显示数据
博客介绍了社区数据的获取与解析。通过Charles截取TabBar切换到“社区”时的GET请求获取数据,包括推广、订阅和热门搜索数据,分别输入对应网址得到JSON数据,之后添加模型数组并解析数据,最后显示数据。
214

被折叠的 条评论
为什么被折叠?



