码迷,mamicode.com
首页 > Web开发 > 详细

UI1_HTTP下载

时间:2015-07-17 13:42:48      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

//  DataModel.h
//  UI1_HTTP下载
//
//  Created by zhangxueming on 15/7/17.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface DataModel : NSObject

@property (nonatomic, copy)NSString *name;
@property (nonatomic, assign)NSInteger downloads;

@end


//  DataModel.m
//  UI1_HTTP下载
//
//  Created by zhangxueming on 15/7/17.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "DataModel.h"

@implementation DataModel

- (NSString *)description
{
    return [NSString stringWithFormat:@"name = %@ downloads = %li", _name, _downloads];
}

@end

 

//  AppDelegate.m
//  UI1_HTTP下载
//
//  Created by zhangxueming on 15/7/17.
//  Copyright (c) 2015年 zhangxueming. 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.
    ViewController *root = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
    self.window.rootViewController = nav;
    self.window.backgroundColor = [UIColor whiteColor];
    return YES;
}

 

//  ViewController.h
//  UI1_HTTP下载
//
//  Created by zhangxueming on 15/7/17.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController


@end

//
//  ViewController.m
//  UI1_HTTP下载
//
//  Created by zhangxueming on 15/7/17.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ViewController.h"
#import "DataModel.h"

//www.baicom.com
//http协议: 超文本传输协议  ftp://
//报文
//HttpURL格式:
//http://  协议类型
//iappfree.candou.com 主机地址
//:8080 端口号
///free/applications/limited? 主机执行程序文件路径
//currency=rmb&page=1  主机执行程序需要的参数 每个参数之间用&隔开

@interface ViewController ()
{
    NSMutableArray *_dataList;
    //NSMutableData *_data;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _dataList = [NSMutableArray array];
    
    [self loadDataWithPage:4];
}

//http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1

//客户端    服务器

- (void)loadDataWithPage:(NSInteger)page
{
    NSString *urlString = [NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=%ld", page];
    
    NSURL *url = [NSURL URLWithString:urlString];
    //下载数据
    NSString *dataString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"dataString = %@", dataString);
    
    //转换成data
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    //json解析
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    if ([result isKindOfClass:[NSDictionary class]]) {
        //字典
        NSArray *app = [result objectForKey:@"applications"];
        //NSLog(@"app= %@", app);
        //[_dataList addObjectsFromArray:app];
        for (NSDictionary *dict in app) {
            DataModel *model = [[DataModel alloc] init];
            NSString *appName = [dict objectForKey:@"name"];
            NSNumber *downloads = [dict objectForKey:@"downloads"];
            model.name  = appName;
            model.downloads = [downloads integerValue];
            [_dataList addObject:model];
        }
        NSLog(@"dataList = %@", _dataList);
    }
    else if([result isKindOfClass:[NSArray class]])
    {
        //数组
    
    }
}

#pragma mark   ---UITableViewDataSource---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
    }

    DataModel *model = (DataModel *)_dataList[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text =[NSString stringWithFormat:@"下载次数:%li",model.downloads];
    return cell;
}



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

@end

 

UI1_HTTP下载

标签:

原文地址:http://www.cnblogs.com/0515offer/p/4653977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!