IOS-MVC层读取服务器接口JSON数据

IOS-MVC层读取服务器接口JSON数据,第1张

概述IOS-MVC层读取服务器接口JSON数据 此篇文章主要讲述获取服务器JSON接口数据,以便大家在今后的物联网信息时代,移动数据肯定都是需要网络数据的,那么就需要用一个分布集中,整体化的,规范性的创建所需的文件,同时方便以后维护: 分别为:M(model),M(manager),V(view),C(cell),C(controller); 具体程序如下: 一:YSMasonryManager.h #import <Foundation/Foundation.h> #import "BaseManager.h&quot ...

此篇文章主要讲述获取服务器JsON接口数据,以便大家在今后的物联网信息时代,移动数据肯定都是需要网络数据的,那么就需要用一个分布集中,整体化的,规范性的创建所需的文件,同时方便以后维护;

分别为:M(model),M(manager),V(vIEw),C(cell),C(controller);

具体程序如下:

一:YSMasonryManager.h

#import <Foundation/Foundation.h>#import "BaseManager.h"#import "YSMasonryModel.h"@protocol YSMasonryManagerDelegate <NSObject>-(voID)networkSuccess:(YSMasonryModel *_Nullable)result;-(voID)networkFail:(Nsstring *_Nullable)result;@endNS_ASSUME_NONNulL_BEGIN@interface YSMasonryManager : BaseManager@property(weak,nonatomic) ID<YSMasonryManagerDelegate> 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 <Foundation/Foundation.h>#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 <Foundation/Foundation.h>#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 <UIKit/UIKit.h>#import "BaseVIEw.h"NS_ASSUME_NONNulL_BEGIN@interface YSMasonryVIEw : BaseVIEw@property(nonatomic,strong) UI@R_403_5991@VIEw *mDevericePile;@endNS_ASSUME_NONNulL_END

八:YSMasonryVIEw.m

#import <Foundation/Foundation.h>#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 = [[UI@R_403_5991@VIEw alloc] init];  _mDevericePile.separatorStyle = UI@R_403_5991@VIEwCellSeparatorStyleSingleline;  [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 <UIKit/UIKit.h>#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:(UI@R_403_5991@VIEwCellStyle)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 <UIKit/UIKit.h>#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) NSMu@R_403_5991@Array *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 <Foundation/Foundation.h>#import "YSMasonryContller.h"#define CELLIDENTIFITER "NET@R_403_5991@VIEWCELL"@interface YSMasonryContller()<YSMasonryManagerDelegate,UI@R_403_5991@VIEwDataSource,UI@R_403_5991@VIEwDelegate,UITextFIEldDelegate>@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.@R_403_5991@FooterVIEw = 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;}-(NSMu@R_403_5991@Array*)dataList{  if(_dataList==nil){    _dataList = [NSMu@R_403_5991@Array 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 = [NSMu@R_403_5991@Array 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 @R_403_5991@VIEw-(NSInteger)@R_403_5991@VIEw:(UI@R_403_5991@VIEw *)@R_403_5991@VIEw numberOfRowsInSection:(NSInteger)section{  return self.dataList.count;}- (UI@R_403_5991@VIEwCell *)@R_403_5991@VIEw:(UI@R_403_5991@VIEw *)@R_403_5991@VIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath {  YSMasonryCell *cell = [@R_403_5991@VIEw dequeueReusableCellWithIDentifIEr:@CELLIDENTIFITER];  if(cell==nil){    cell = [[YSMasonryCell alloc] initWithStyle:UI@R_403_5991@VIEwCellStyleDefault 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)@R_403_5991@VIEw:(UI@R_403_5991@VIEw *)@R_403_5991@VIEw 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)@R_403_5991@VIEw:(UI@R_403_5991@VIEw *)@R_403_5991@VIEw 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 <Foundation/Foundation.h>#define BASE_URL @"http://110.22.33/ilLoRaWan/"#define USERJWTKEYS @"eyJhbGciOiJIUzI1NIISInR5cCI6IkpXVCJ9.eyJpc3MiOiJsb3JhLWFwcC1zZXJ2ZXIiLCJhDWQiOiJsb3JhLWFwc"

至此!

以上详细的代码,主要就是mode数据层那,修改自己的Json的返回数据,与一些字典类型的转换,就没其他的难点了。这样写的主要目的是,思路逻辑更清晰,后期维护也简单,分开着写代码更简洁。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

总结

以上是内存溢出为你收集整理的IOS-MVC层读取服务器接口JSON数据全部内容,希望文章能够帮你解决IOS-MVC层读取服务器接口JSON数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1107961.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存