此篇文章主要講述獲取服務(wù)器JSON接口數(shù)據(jù),以便大家在今后的物聯(lián)網(wǎng)信息時(shí)代,移動(dòng)數(shù)據(jù)肯定都是需要網(wǎng)絡(luò)數(shù)據(jù)的,那么就需要用一個(gè)分布集中,整體化的,規(guī)范性的創(chuàng)建所需的文件,同時(shí)方便以后維護(hù);
10年積累的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有蒸湘免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
分別為: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; @end NS_ASSUME_NONNULL_BEGIN @interface YSMasonryManager : BaseManager @property(weak,nonatomic) id<YSMasonryManagerDelegate> delegate; -(void)getDeviceProile:(NSString *)limit Offset:(NSString *)offset; @end NS_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"http://頭部manager對(duì)象 #import "YSMasonryModel.h"http://模型層
三: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; @end NS_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; @end NS_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) UITableView *mDevericePile; @end NS_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 = [[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 <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; @end NS_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名稱(chēng) _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); }]; //創(chuàng)建時(shí)間 _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); }]; //端口號(hào) _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) 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; @end NS_ASSUME_NONNULL_END
十二:YSMasonryContller.m
#import <Foundation/Foundation.h> #import "YSMasonryContller.h" #define CELLIDENTIFITER "NETTABLEVIEWCELL" @interface YSMasonryContller()<YSMasonryManagerDelegate,UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate> @end @implementation YSMasonryContller - (void)viewDidLoad { [super viewDidLoad]; [self initData]; } //頭部標(biāo)題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; } //啟動(dòng)時(shí)的運(yùn)行數(shù)據(jù) - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; //加載數(shù)據(jù) 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設(shè)備 -(void)onAddClick{ } @end
十三:所用到的常量一些定義:
Constants.h
#import <Foundation/Foundation.h> #define BASE_URL @"http://110.22.33/ilLoRaWan/" #define USERJWTKEYS @"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJsb3JhLWFwcC1zZXJ2ZXIiLCJhdWQiOiJsb3JhLWFwc"
至此!
以上詳細(xì)的代碼,主要就是mode數(shù)據(jù)層那,修改自己的json的返回?cái)?shù)據(jù),與一些字典類(lèi)型的轉(zhuǎn)換,就沒(méi)其他的難點(diǎn)了。這樣寫(xiě)的主要目的是,思路邏輯更清晰,后期維護(hù)也簡(jiǎn)單,分開(kāi)著寫(xiě)代碼更簡(jiǎn)潔。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
標(biāo)題名稱(chēng):IOS-MVC層讀取服務(wù)器接口JSON數(shù)據(jù)
網(wǎng)頁(yè)URL:http://www.rwnh.cn/article46/jjsshg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)