千家信息网

IOS中MVC层如何读取服务器接口JSON数据

发表于:2024-11-19 作者:千家信息网编辑
千家信息网最后更新 2024年11月19日,这篇文章主要介绍了IOS中MVC层如何读取服务器接口JSON数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体程序如下:一:YSM
千家信息网最后更新 2024年11月19日IOS中MVC层如何读取服务器接口JSON数据

这篇文章主要介绍了IOS中MVC层如何读取服务器接口JSON数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

具体程序如下:

一:YSMasonryManager.h

#import #import "BaseManager.h"#import "YSMasonryModel.h"@protocol YSMasonryManagerDelegate -(void)networkSuccess:(YSMasonryModel *_Nullable)result;-(void)networkFail:(NSString *_Nullable)result;@endNS_ASSUME_NONNULL_BEGIN@interface YSMasonryManager : BaseManager@property(weak,nonatomic) id delegate;-(void)getDeviceProile:(NSString *)limit Offset:(NSString *)offset;@endNS_ASSUME_NONNULL_END

二:YSMasonryManager.m

#import "YSMasonryManager.h"@implementation YSMasonryManager-(void)getDeviceProile:(NSString *)limit Offset:(NSString *)offset{ NSString *strUrl = [NSString stringWithFormat:@"%@api/internal/profile",DEVICE_BASE_URL]; //JSON [PPNetworkHelper setRequestSerializer:PPRequestSerializerHTTP]; [PPNetworkHelper setResponseSerializer:PPResponseSerializerJSON]; //配置Jwt文件 [PPNetworkHelper setValue:USERJWTKEYS forHTTPHeaderField:@"Grpc-Metadata-Authorization"]; [PPNetworkHelper GET:strUrl parameters:@"" success:^(id responseObject){ YSMasonryModel *net = [YSMasonryModel mj_objectWithKeyValues:responseObject]; if(net.organizations.count>0){ if([self.delegate respondsToSelector:@selector(networkSuccess:)]){ [self.delegate networkSuccess:net]; } }else{ if([self.delegate respondsToSelector:@selector(networkFail:)]){ [self.delegate networkFail:@"Get Fail!"]; } } } failure:^(NSError *error){ // NSError *newError = [NSError returnErrorWithError:error]; if([self.delegate respondsToSelector:@selector(networkFail:)]){ [self.delegate networkFail:@"cc"]; } }];}@end

分析:

#import "BaseManager.h"//头部manager对象#import "YSMasonryModel.h"//模型层

三:YSMasonryModel.h

#import #import "BaseModel.h"#import "YSMasonryPileModel.h"NS_ASSUME_NONNULL_BEGIN@interface YSMasonryModel : BaseModel@property(nonatomic, copy) NSString *settings;@property(nonatomic, copy) NSArray *organizations;@property(nonatomic, copy) NSString *user;+(NSDictionary*)mj_objectClassInArray;@endNS_ASSUME_NONNULL_END

四:YSMasonryModel.m

#import "YSMasonryModel.h"@implementation YSMasonryModel+(NSDictionary*)mj_objectClassInArray{ return @{@"organizations":[YSMasonryPileModel class],};}@end

五:YSMasonryPileModel.h

#import #import "BaseModel.h"NS_ASSUME_NONNULL_BEGIN@interface YSMasonryPileModel : BaseModel@property(nonatomic,assign) NSString *isAdmin;@property(nonatomic, copy) NSString *organizationID;@property(nonatomic, copy) NSString *organizationName;@property(nonatomic, copy) NSString *createdAt;@property(nonatomic, copy) NSString *updatedAt;@property(nonatomic,assign) NSString *disableAssignExistingUsers;@property(nonatomic, copy) NSString *email;@property(nonatomic, copy) NSString *uid;@property(nonatomic,assign) NSString *isActive;@property(nonatomic, copy) NSString *note;@property(nonatomic,assign) NSString *sessionTTL;@property(nonatomic, copy) NSString *username;-(id)initWithDict:(NSDictionary*)dict;@endNS_ASSUME_NONNULL_END

六:YSMasonryPileModel.m

#import "YSMasonryPileModel.h"@implementation YSMasonryPileModel+ (NSDictionary *)mj_replacedKeyFromPropertyName{ return @{@"uid": @"uid"};}-(id)initWithDict:(NSDictionary*)dict{ if(self = [super init]){ self.createdAt = dict[@"createdAt"]; //@"gatewayDiscoveryEnabled":@(enable), self.isAdmin = dict[@"isAdmin"]; self.organizationID = dict[@"organizationID"]; self.organizationName = dict[@"networkServerID"]; self.updatedAt = dict[@"updatedAt"]; self.disableAssignExistingUsers = dict[@"disableAssignExistingUsers"]; self.email = dict[@"email"]; self.uid = dict[@"id"]; self.isActive = dict[@"isActive"]; self.note = dict[@"note"]; self.sessionTTL = dict[@"sessionTTL"]; self.username = dict[@"username"]; } return self;}@end

七:YSMasonryView.h

#import #import "BaseView.h"NS_ASSUME_NONNULL_BEGIN@interface YSMasonryView : BaseView@property(nonatomic,strong) UITableView *mDevericePile;@endNS_ASSUME_NONNULL_END

八:YSMasonryView.m

#import #import "YSMasonryView.h"#ifdef __OBJC__//define this constant if you want to use Masonry without the 'mas_' prefix#define MAS_SHORTHAND//define this constant if you want to enable auto-boxing for default syntax#define MAS_SHORTHAND_GLOBALS#import "Masonry.h"#endif@implementation YSMasonryView-(void)initView{ _mDevericePile = [[UITableView alloc] init]; _mDevericePile.separatorStyle = UITableViewCellSeparatorStyleSingleLine; [self addSubview:_mDevericePile]; [_mDevericePile makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(0); make.left.equalTo(self).offset(0); make.right.equalTo(self).offset(0); make.bottom.equalTo(self).offset(0); }];}@end

九:YSMasonryCell.h

#import #import "BaseCell.h"NS_ASSUME_NONNULL_BEGIN@interface YSMasonryCell : BaseCell@property(nonatomic, strong) UIImageView *mNetImgVw;@property(nonatomic, strong) UILabel *mNetNameLb;@property(nonatomic, strong) UILabel *mNetServerLb;@property(nonatomic, strong) UILabel *mNetTimeLb;@endNS_ASSUME_NONNULL_END

十:YSMasonryCell.m

#import "YSMasonryCell.h"@implementation YSMasonryCell- (void)awakeFromNib { [super awakeFromNib];}- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if(self){ UIView *mainView = [[UIView alloc] init]; mainView.layer.cornerRadius = 8; mainView.layer.borderColor = [[UIColor grayColor] CGColor]; mainView.layer.borderWidth = 1; [self addSubview:mainView]; [mainView makeConstraints:^(MASConstraintMaker *make){ make.left.equalTo(self).offset(10); make.right.equalTo(self).offset(-10); make.top.equalTo(self).offset(10); make.bottom.equalTo(self).offset(-10); }]; _mNetImgVw = [[UIImageView alloc] init]; [mainView addSubview:_mNetImgVw]; [_mNetImgVw makeConstraints:^(MASConstraintMaker *make){ make.centerY.equalTo(self); make.left.equalTo(self).offset(20); make.width.equalTo(@25); make.height.equalTo(@25); }]; //Net名称 _mNetNameLb = [[UILabel alloc] init]; _mNetNameLb.textColor = [UIColor grayColor]; _mNetNameLb.font = [UIFont fontWithName:@"Helvetica-Bold" size:16]; _mNetNameLb.textAlignment = NSTextAlignmentLeft; [mainView addSubview:_mNetNameLb]; [_mNetNameLb makeConstraints:^(MASConstraintMaker *make){ make.top.equalTo(mainView).offset(5); make.left.equalTo(self.mNetImgVw).offset(35); make.right.equalTo(mainView).offset(-5); make.height.equalTo(@30); }]; //创建时间 _mNetTimeLb = [[UILabel alloc] init]; _mNetTimeLb.textColor = [UIColor grayColor]; _mNetTimeLb.font = [UIFont fontWithName:@"Helvetica-Bold" size:14]; _mNetTimeLb.textAlignment = NSTextAlignmentRight; [mainView addSubview:_mNetTimeLb]; [_mNetTimeLb makeConstraints:^(MASConstraintMaker *make){ make.top.equalTo(mainView).offset(10); make.right.equalTo(mainView).offset(-10); make.width.equalTo(@160); make.height.equalTo(@30); }]; //端口号 _mNetServerLb = [[UILabel alloc] init]; _mNetServerLb.textColor = [UIColor grayColor]; _mNetServerLb.font = [UIFont fontWithName:@"Helvetica-Bold" size:16]; _mNetServerLb.textAlignment = NSTextAlignmentLeft; [mainView addSubview:_mNetServerLb]; [_mNetServerLb makeConstraints:^(MASConstraintMaker *make){ make.bottom.equalTo(self).offset(-15); make.left.equalTo(self.mNetImgVw).offset(35); make.right.equalTo(mainView).offset(-10); make.height.equalTo(@30); }]; } return self;}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated];}@end

十一:YSMasonryContller.h

#import #import "BaseViewController.h"#import "YSMasonryView.h"#import "YSMasonryCell.h"#import "YSMasonryManager.h"#import "YSMasonryPileModel.h"#import "MBProgressHUD.h"#import "MJRefresh.h"#import "WHToast.h"NS_ASSUME_NONNULL_BEGIN@interface YSMasonryContller : BaseViewController@property(nonatomic,strong) YSMasonryView *lgdView;@property(nonatomic, strong) YSMasonryManager *lgdManager;@property(strong, nonatomic) NSMutableArray *dataList;@property (strong,nonatomic) MBProgressHUD *hud;@property(nonatomic, strong) YSMasonryPileModel *model;@property(nonatomic, strong) NSString *organizations;@property(assign,nonatomic) int limit;@property(assign,nonatomic) int offsert;@endNS_ASSUME_NONNULL_END

十二:YSMasonryContller.m

#import #import "YSMasonryContller.h"#define CELLIDENTIFITER "NETTABLEVIEWCELL"@interface YSMasonryContller()@end@implementation YSMasonryContller- (void)viewDidLoad { [super viewDidLoad]; [self initData];}//头部标题a- (void)customContentView{ UIColor *commonBlue = [self.commonUtil stringToColor:@"#333333"]; [self.navigationController.navigationBar setBarTintColor:commonBlue]; self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; self.navigationItem.title = @"NetWork Server";}- (void)customNavigationLeftItem{ UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = item;}-(void)customNavigationRightItem{ UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(onAddClick)]; add.tintColor = [UIColor whiteColor]; self.navigationItem.rightBarButtonItem = add;}-(void)initData{ UIView *view = [[UIView alloc] init]; self.lgdView.mDevericePile.tableFooterView = view; self.lgdView.frame = self.view.bounds; [self.view addSubview:self.lgdView]; _limit = 5; _offsert = 0;}//启动时的运行数据- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; //加载数据 NSString *slimit = [NSString stringWithFormat:@"%d",_limit]; NSString *soffset = [NSString stringWithFormat:@"%d",_offsert]; [self.lgdManager getDeviceProile:slimit Offset:soffset];}-(YSMasonryView*)lgdView{ if(_lgdView==nil){ _lgdView = [[YSMasonryView alloc] init]; _lgdView.mDevericePile.dataSource = (id)self; _lgdView.mDevericePile.delegate = (id)self; } return _lgdView;}-(YSMasonryManager*)lgdManager{ if(_lgdManager==nil){ _lgdManager = [[YSMasonryManager alloc] init]; _lgdManager.delegate = (id)self; } return _lgdManager;}-(NSMutableArray*)dataList{ if(_dataList==nil){ _dataList = [NSMutableArray array]; } return _dataList;}#pragma mark get network data-(void)networkSuccess:(YSMasonryModel *_Nullable)result{ [self.lgdView.mDevericePile.mj_header endRefreshing]; [self.lgdView.mDevericePile.mj_footer endRefreshing]; self.dataList = [NSMutableArray arrayWithArray:result.organizations]; [self.lgdView.mDevericePile reloadData];}-(void)networkFail:(NSString *_Nullable)result{ [self.lgdView.mDevericePile.mj_header endRefreshing]; [self.lgdView.mDevericePile.mj_footer endRefreshing]; [WHToast showMessage:result originY:500 duration:2 finishHandler:^{ }];}#pragma mark tableView-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataList.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { YSMasonryCell *cell = [tableView dequeueReusableCellWithIdentifier:@CELLIDENTIFITER]; if(cell==nil){ cell = [[YSMasonryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@CELLIDENTIFITER]; } if(_dataList.count>0){ YSMasonryPileModel *model = [[YSMasonryPileModel alloc] initWithDict:[self.dataList objectAtIndex:indexPath.row]]; if(model!=nil){ //cell.mNetImgVw.image = [UIImage imageNamed:@"net"]; // cell.mNetNameLb.text = model.isAdmin; // cell.mNetTimeLb.text = [self dateAndStrings:model.createdAt]; cell.mNetServerLb.text = @"vv"; } } return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if(_dataList.count>0){ YSMasonryPileModel *model = [[YSMasonryPileModel alloc] initWithDict:[self.dataList objectAtIndex:indexPath.row]]; if(model!=nil){ // [self updateNetWorkServer:model.deviceprolieid]; } }}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 100;}-(NSString*)dateAndStrings:(NSString*)date{ if(date!=nil){ NSArray *strtime = [date componentsSeparatedByString:@"T"]; //NSString *strtime = [date stringByReplacingOccurrencesOfString:@"T" withString:@" "]; return strtime[0]; } return @"None";}//添加add设备-(void)onAddClick{}@end

十三:所用到的常量一些定义:

Constants.h

#import #define BASE_URL @"http://110.22.33/ilLoRaWan/"#define USERJWTKEYS @"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJsb3JhLWFwcC1zZXJ2ZXIiLCJhdWQiOiJsb3JhLWFwc"

感谢你能够认真阅读完这篇文章,希望小编分享的"IOS中MVC层如何读取服务器接口JSON数据"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

数据 篇文章 接口 服务器 服务 头部 价值 兴趣 口号 同时 名称 对象 常量 文件 时间 更多 朋友 标题 模型 知识 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 企业数据库 软件开发 定制软件开发及系统迁移 特岗服务器有绩效吗 hive元数据库登录 轻云服务器安全吗 如何使用国泰君安数据库 奉贤区自制网络技术 驱动软件开发是做什么的 深圳网络技术开发怎么样 中国最好的服务器是什么 对于数据库的了解和认识 云服务器节约费用 刑事警察学院网络安全与执法 网络技术资源网站 东华科技做什么互联网经营 左晓栋网络安全人才培养 九龙坡区网络软件开发流程报价表 云时代环境对软件开发的影响 河北质量软件开发服务保障 avast网络安全key 数据库中count是啥函数 加强网络安全工作的思考 怎么把我的世界材质放到服务器里 广东嵌入式软件开发服务费 广州淋漓尽致网络技术有限公司 软件开发公司如何运作 软件开发服务框架协议范本 我是网络安全小卫士绘画 很有趣的服务器 数据库中的表由表内容和什么组成
0