JsonModel&AFNetWorking

//  HttpManager.h
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AFNetworking.h"
typedef void  (^sucessBlock)(AFHTTPRequestOperation *operation, id responseObject);
typedef void  (^faileBlock)(AFHTTPRequestOperation *operation, NSError *error);
@interface HttpManager : NSObject

+ (HttpManager *)shareManager;
- (void)requestWithUrl:(NSString *)url withDic:(NSDictionary *)dic WithSuccess:(sucessBlock)sucBlock WithFailure:(faileBlock) failBlock;
@end

//
//  HttpManager.m
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "HttpManager.h"

@implementation HttpManager

+ (HttpManager *)shareManager
{
    static HttpManager *manager;
    static dispatch_once_t once;
    dispatch_once(&once,^{

    if(manager ==nil)
    {
        manager =[[HttpManager alloc]init];
    }

    });
    return manager;

//    static dispatch_once_t *once;   错误
//    dispatch_once(once,^{

}

- (void)requestWithUrl:(NSString *)url withDic:(NSDictionary *)dic WithSuccess:(sucessBlock)sucBlock WithFailure:(faileBlock) failBlock {
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];

    //manager.responseSerializer.acceptableContentTypes =[NSSet setWithObject:@"application/json"];

     manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json",@"text/text", nil];
    [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        sucBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
        failBlock(operation,error);

    }];
}
@end
//
//  GFControl.h
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface GFControl : NSObject
+ (UILabel *)createLabelWithFrame:(CGRect)frame WithFont:(UIFont *)font;
@end

//
//  GFControl.m
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "GFControl.h"

@implementation GFControl

+ (UILabel *)createLabelWithFrame:(CGRect)frame WithFont:(UIFont *)font
{
    UILabel *label =[[UILabel alloc]initWithFrame:frame];
    label.textColor=[UIColor blackColor];
    label.font=font;
    label.textAlignment=NSTextAlignmentLeft;
    return label;
}
@end
//
//  MyModel.h
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "JSONModel.h" //创建的时候继承至JSONModel
//#import "JSONModelLib.h"

//@protocol CreateDateModel
//@end
@protocol ObjModel
@end

@interface CreateDateModel : JSONModel

@property (nonatomic,strong) NSString <Optional> *date;
@property (nonatomic,strong) NSString <Optional> *day;
@property (nonatomic,strong) NSString <Optional> *hours;
@property (nonatomic,strong) NSString <Optional> *minutes;
@property (nonatomic,strong) NSString <Optional> *month;
@property (nonatomic,strong) NSString <Optional> *seconds;
@property (nonatomic,strong) NSString <Optional> *time;
@property (nonatomic,strong) NSString <Optional> *timezoneOffset;
@property (nonatomic,strong) NSString <Optional> *year;
@end

@interface ObjModel : JSONModel

@property (nonatomic,strong) NSString <Optional> *descriptionOne;
@property (nonatomic,strong) NSString <Optional> *isNew;
@property (nonatomic,strong) NSString <Optional> *isRecommend;
@property (nonatomic,strong) NSString <Optional> *isSubscibe;
@property (nonatomic,strong) NSString <Optional> *itemID;
@property (nonatomic,strong) NSString <Optional> *itemName;
@property (nonatomic,strong) NSString <Optional> *picUrl;
@property (nonatomic,strong) NSString <Optional> *readNum;
//@property (nonatomic,strong) NSString <Optional> *numRows;
//@property (nonatomic,strong) NSArray <Optional,CreateDateModel> *obj;

@property (nonatomic,strong) CreateDateModel <Optional> *createDate;
@end

@interface MyModel : JSONModel

@property (nonatomic,strong) NSString <Optional> *numRows;
@property (nonatomic,strong) NSDictionary <Optional> *resultObj;
@property (nonatomic,strong) NSArray <Optional,ObjModel> *obj;
@end

//
//  MyModel.m
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "MyModel.h"

@implementation CreateDateModel

@end

@implementation ObjModel

+(JSONKeyMapper *)keyMapper
{
//    NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
//    [dic setObject:@"description" forKey:@"description1"];
//    return [[JSONKeyMapper alloc]initWithDictionary:dic];

    return [[JSONKeyMapper alloc]initWithDictionary:@{@"description":@"descriptionOne"}];
}

@end

@implementation MyModel

@end
//  MyCell.h
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell

@property (nonatomic,strong)UILabel *itemNameLabel;
@property (nonatomic,strong)UILabel *desLabel;
@property (nonatomic,strong)UILabel *timeLabel;
@property (nonatomic,strong)UIImageView *psImage;

@end

//
//  MyCell.m
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "MyCell.h"
#import "GFControl.h"
@implementation MyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self  configCell];
    }

    return self;
}

- (void)configCell
{
    _psImage =[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80, 90)];
    [self addSubview:_psImage];

    CGFloat witdth=[[UIScreen mainScreen] bounds].size.width;

    _itemNameLabel =[GFControl createLabelWithFrame:CGRectMake(110, 10, witdth-100-10, 30) WithFont:[UIFont systemFontOfSize:15]];
   // NSLog(@"%@",_itemNameLabel.text);

    [self addSubview:_itemNameLabel];

    _desLabel =[GFControl createLabelWithFrame:CGRectMake(110, 40, witdth-100-10, 30) WithFont:[UIFont systemFontOfSize:13]];
    [self addSubview:_desLabel];

    _timeLabel =[GFControl createLabelWithFrame:CGRectMake(110, 70, witdth-100-10, 30) WithFont:[UIFont systemFontOfSize:13]];
    _timeLabel.textAlignment=NSTextAlignmentRight;
    [self addSubview:_timeLabel];
}
@end
//  ViewController.h
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

//
//  ViewController.m
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "ViewController.h"
#import "HttpManager.h"
#import "MyModel.h"
#import "MyCell.h"
#import "UIImageView+WebCache.h"
#define KPostUrl @"http://services.baidu.com/ApricotForestWirelessServiceForLiterature/LiteratureDataServlet"
@interface ViewController ()  <UITableViewDataSource,UITableViewDelegate>
{
    MyModel *_myModel;
    UITableView *_tabelView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.view.backgroundColor=[UIColor whiteColor];
//
//    [[HttpManager shareManager]  requestWithUrl:@"" withDic:nil WithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//    } WithFailure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
//    }];
    [self  configUI];
    [self requestdata];

}

- (void)configUI
{
    _tabelView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:_tabelView];
    _tabelView.delegate=self;
    _tabelView.dataSource=self;

    [_tabelView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCell"];

}

- (void)requestdata
{
    NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
    [dic setObject:@"7B63373363386530373034366164393262633633373039326138356238366366307D2C7B336464363563386338626263653130663531623832616632343963363832323065363964656161627D2C7B66616C73657D2C7B307D2C7B307D2C7B66373539333863642D326162352D346232342D616336622D3132386538626434663366397D2C7B37363033396665333533326461623034656561353162363734643131636532617D2C7B323031352D30332D30372031313A31383A33337D2C7B312E382E367D2C7B696F736C6974657261747572657D2C7B494F536C6974657261747572655F312E382E365F696F73382E312E335F6950686F6E65352D327D" forKey:@"sessionKey"];
    [dic setObject:@"10" forKey:@"pageSize"];
    [dic setObject:@"2" forKey:@"rtype"];
    [dic setObject:@"0" forKey:@"pageIndex"];
    [dic setObject:@"getLiteraturegroupBySpecialty" forKey:@"m"];
    [[HttpManager shareManager] requestWithUrl:KPostUrl withDic:dic WithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        _myModel =[[MyModel alloc] initWithDictionary:responseObject error:nil];

        //NSLog(@"%@",responseObject);
        [_tabelView reloadData];

        NSLog(@"%@",_myModel.numRows);
    } WithFailure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _myModel.obj.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    MyCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    MyCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    ObjModel *obj=_myModel.obj[indexPath.row];

    [cell.psImage sd_setImageWithURL:[NSURL URLWithString:obj.picUrl]];
    cell.desLabel.text =obj.descriptionOne;
    cell.itemNameLabel.text=obj.itemName;
    cell.timeLabel.text=[NSString stringWithFormat:@"%@时%@分%@秒",obj.createDate.hours,obj.createDate.minutes,obj.createDate.seconds];

    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
//  AppDelegate.h
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

//Utility:自己封装的类
//libs:第3方库
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

//  AppDelegate.m
//  JsonModel&AFNetWorking
//
//  Created by qianfeng on 15/7/21.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    CGRect frame =[[UIScreen mainScreen] bounds];//获取屏幕尺寸
    self.window =[[UIWindow alloc]initWithFrame: frame];
    self.window.backgroundColor =[UIColor clearColor];
    [self.window makeKeyAndVisible]; //让window可见
    //以上三句,创建空模板

    ViewController *vc =[[ViewController alloc]init];
    self.window.rootViewController=vc;

    return YES;
}

时间: 2024-10-19 23:38:56

JsonModel&AFNetWorking的相关文章

HTTPPost/AFNetWorking/JSONModel/NSPredicate

一.HTTPPost================================================ 1. POST方式发送请求 HTTP协议下默认数据发送请求方法是GET方式,若需要使用POST方法,则需要对发送的请求也就是request对象,进行属性设置. 步骤如下: > 要发送的请求对象,需要使用可变请求对象 [[NSMutableURLRequest alloc] initWithURL:] 此时创建的请求对象,其相关属性是可以进行设置的,通过NSURLRequest创建

CocoaPods报错:The dependency `AFNetworking ` is not used in any concrete target

最近更新了下cocoapods,今天再pod update  就遇到这个错误: 大体意思就是说,库没有用到指定的target. 找了下资料,发现是新版CocoaPods在 Podfile里使用时,必须指定target: 比如: platform:ios,'7.0' target 'TestSwift' do pod 'AFNetworking' pod 'SDWebImage' pod 'JSONModel' pod 'MJRefresh' pod 'SDCycleScrollView','~>

AFNetworking 2.0 Tutorial

Update 1/18/2014: Fully updated for iOS 7 and AFNetworking 2.0 (original post by Scott Sherwood, update by Joshua Greene). In iOS 7, Apple introduced NSURLSession as the new, preferred method of networking (as opposed to the older NSURLConnection API

ios AFNetworking 3.0 原码阅读分析 (一)(AFURLRequestSerialization)

本文主要内容是讲AFNetworking中的AFURLRequestSerialization.它主要的作用是在我们要发送一个网络请求的时候帮助我们创NSMutableURLRequest并封装好所需要的参数到NSMutableURLRequest中.那它内部做了些什么,提供了什么功能,使得我们进行网络请求时候变得如此方便.简单.好像我们什么都不用管就能建立一个正确的请求体NSURLRequest.接下来就会一步步揭开它神秘的面纱. 概览 首先看一下在AFURLRequestSerializat

AFNetworking 3.0x版本最新特性

AFNetworking是一款在OS X和iOS下都令人喜爱的网络库.为了迎合iOS新版本的升级, AFNetworking在3.0版本中删除了基于 NSURLConnection API的所有支持.如果你的项目以前使用过这些API,建议您立即升级到基于 NSURLSession 的API的AFNetworking的版本.本指南将引导您完成这个过程. 本指南是为了引导使用AFNetworking 2.x升级到最新的版本API,以达到过渡的目的,并且解释了新增和更改的设计结构. 新设备要求: iO

AFNetworking发送和接收字符串时报3840错误的解决办法

AFNetworking框架默认请求类型和响应类型都是JSON格式的,在特殊情况下,我们使用字符串形式的时候,就会报3840错误,如何解决呢? 设置请求管理者: // 因为传递过去和接收回来的数据都不是json类型的,所以在这里要设置为AFHTTPRequestSerializer和AFHTTPResponseSerializer mgr.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求 mgr.responseSeri

iOS开发 之 AFNetworking的基本使用

首先下载AFNetworking 然后在需要使用的类中,导入如下2个头文件: #import "AFNetworking.h"//主要用于网络请求方法 #import "UIKit+AFNetworking.h"//里面有异步加载图片的方法 GET请求网络数据方式: -(void)obtainData {     // 启动系统风火轮     [UIApplication sharedApplication].networkActivityIndicatorVisi

AFNetworking imageView button设置图片缓存

如果对AFNetworking 下载的图片不进行缓存,可能会导致每次加载  image图片的时候都会重新 下载图片; 严重浪费资源: AFNetworking有自带的很方便的配置缓存图片的方法: 在 UIKit+AFNetworking中, imageView和Button的类目都可以设置缓存: 方法如下: button [UIButton setSharedImageCache:[UIButton sharedImageCache]]; imageView [UIImageView setSh

AFNetworking 2.5.0版本的使用

http://afnetworking.com/ http://cocoadocs.org/docsets/AFNetworking/2.5.0/ 1. 下载源码并进行编译 源码地址 http://pan.baidu.com/s/1jG24w3W 2. 判断当前是否有网络,基准网址为 http://baidu.com/ 源码: 可以用来监测WWAN或者WiFi或者断网状态;) // 基准baseURL NSURL *baseURL = [NSURL URLWithString:@"http://