zoukankan      html  css  js  c++  java
  • 使用ASIHTTPRequest获得天气信息

    使用ASIHTTPRequest+JSON一个简单的天气获取Demo,直接上代码:

    工程结构截图如下:

    加入ASIHTTPRequest的方法请查看上一篇文件,ViewController.h代码如下:

     1 //
    2 // ViewController.h
    3 // ASIHTTPRequestDemo
    4 //
    5 // Created by Fox on 12-3-13.
    6 // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
    7 //
    8
    9 #import <UIKit/UIKit.h>
    10
    11 @interface ViewController : UIViewController
    12 {
    13 NSDictionary *cityWeather;
    14
    15 IBOutlet UILabel *city;
    16 IBOutlet UILabel *weather;
    17 }
    18
    19 - (IBAction)getWeather:(id)sender;
    20 -(void)showWeather;
    21 @property(nonatomic, retain) NSDictionary *cityWeather;
    22
    23 @end

    ViewController.m如下:

      1 //
    2 // ViewController.m
    3 // ASIHTTPRequestDemo
    4 //
    5 // Created by Fox on 12-3-13.
    6 // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
    7 //
    8
    9 #import "ViewController.h"
    10 #import "JSON.h"
    11 #import "ASIHTTPRequest.h"
    12
    13 @interface ViewController ()
    14
    15 @end
    16
    17 @implementation ViewController
    18
    19 @synthesize cityWeather;
    20
    21 - (void)viewDidLoad
    22 {
    23 [super viewDidLoad];
    24 // Do any additional setup after loading the view, typically from a nib.
    25 }
    26
    27 - (void)viewDidUnload
    28 {
    29 [city release];
    30 city = nil;
    31 [weather release];
    32 weather = nil;
    33 [super viewDidUnload];
    34 // Release any retained subviews of the main view.
    35 }
    36
    37 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    38 {
    39 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    40 }
    41
    42 - (void)dealloc {
    43 [city release];
    44 [weather release];
    45 [super dealloc];
    46 }
    47
    48 - (IBAction)getWeather:(id)sender {
    49
    50 //use the opensourse ASIHTTPRequest to work with a network
    51 //get a city's weather
    52 NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101270101.html"];
    53 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    54 [request setDelegate:self];
    55 [request startAsynchronous];
    56
    57
    58 }
    59
    60 -(void)showWeather{
    61 [city setText:[NSString stringWithFormat:@"%@",[cityWeather objectForKey:@"city"]]];
    62
    63 // NSLog(@"cityName is %@",[cityWeather objectForKey:@"city"]);
    64
    65 [weather setText:[NSString stringWithFormat:@"%@",[cityWeather objectForKey:@"weather2"]]];
    66
    67 }
    68
    69 //call this method when request finished
    70 - (void)requestFinished:(ASIHTTPRequest *)request
    71 {
    72 // NSLog(@"the network is OK...");
    73
    74 SBJSON *parser = [[SBJSON alloc] init];
    75
    76 // Use when fetching text data
    77 NSString *responseString = [request responseString];
    78 // NSLog(@"the responseString is %@",responseString);
    79
    80 // Use when fetching binary data
    81 NSData *responseData = [request responseData];
    82
    83 // NSLog(@"the responseData is %@",responseData);
    84
    85 NSDictionary *dic = [[NSDictionary alloc] initWithDictionary:[parser objectWithString:responseString error:nil]];
    86
    87
    88 self.cityWeather= [dic objectForKey:@"weatherinfo"];
    89
    90
    91 [self showWeather];
    92
    93 [parser release];
    94 }
    95
    96
    97 - (void)requestFailed:(ASIHTTPRequest *)request
    98 {
    99 NSError *error = [request error];
    100 NSLog(@"the error is %@",error);
    101 }
    102
    103
    104 @end



  • 相关阅读:
    spring-boot-swagger2 使用手册
    mall整合Swagger-UI实现在线API文档
    MyBatis Generator 详解
    1046 划拳 (15分)
    1043 输出PATest (20分)
    1042 字符统计 (20分)
    1041 考试座位号 (15分)
    1040 有几个PAT (25分)
    1039 到底买不买 (20分)
    1038 统计同成绩学生 (20分)
  • 原文地址:https://www.cnblogs.com/foxmin/p/2393539.html
Copyright © 2011-2022 走看看