网络请求 新闻翻翻看 (OC UINavigationController NSURL UITableView UIWebView )

来源:互联网 发布:mysqldump恢复数据库 编辑:程序博客网 时间:2024/05/16 11:27

     AppDelegate.h              strong---retain

     AppDelegate.m              声明头文件        #import  "MainViewController.h"

 {

      MainViewController *mainVC =  [[MainViewController alloc] init];

      UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC];

      self.window.rootViewController = naviVC;

      [mainVC release];

      [naviVC release];

      [_window release];

 } 


  - (void)dealloc

 {

       [_window release];  

       [super dealloc];

 }  


     MainViewController.h         <UITableViewDataSource, UITableViewDelegate>  // UITableView 的两个方法

      三个属性

      @property (nonatomic, retain)UITableView *tableView;

      @property (nonatomic, retain)NSMutableData *data;

      @property (nonatomic, retain)NSMutableArray *array;

      MainViewController.m        #import "FirstViewController.h"


 - (id)

{

      self.title = @"北京新闻";

      // 初始化方法

      [self songxueqian];

}  


- (void)viewDidLoad

{

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 , 320, 480) style:UITableViewStylePlain];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];

    [self.tableView release];


     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellQQQ"];


- (NSInteger)tableVIew:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

      [return self.array count];  

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{    

        static NSString *str = @"cellQQQ";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

         cell .textLabel.text = [[self.array objectAtIndex:indexPath.row] objectForKey:@"title"];

         return cell;



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

           FirstViewController *firstVC = [[FirstViewController alloc] init];

           firstVC.string = [[self.array objectAtIndex:indexPath.row] objectForKey:@"newsUrl"];

           [self.navigationController pushViewController:firstVC animated:YES];  

           [firstVC release];    

}



- (void)songxueqian

{      

          NSString *str = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";

          [NSURL *url = NSURL URLWithString:str];

          NSMutableURLRequest *request = NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.f];

         [request setHTTPMethod:@"POST"];

          NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";

     NSData *data = [bodyStr dataUsingEncoding:NSUTF8StingEncoding];

     [request setHTTPBody:data];

     NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

     id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

     self.array = [object objectForKey:@"news"];

     [self.tableView reloadData];

    }];

} 


         FirstViewController.h       

        @property (nonatomic, retain)UIWebView *webView;

        @property (nonatomic, copy)NSMutableString *string;


           FirstViewController.m

 - (id)

{

     self.webView = [[UIWebView alloc] init];

}


- (void)viewDidLoad

{

      self.webView = [UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

       NSURL *url = [NSURL URLWithString:self.string];

       NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.f];

       [self.webView loadRequest:request];

        [self.view addSubview:self.webView];

        [self.webView release];









0 0
原创粉丝点击