使用异步Get在新浪微博上获取最新的微博

本文介绍了一个使用Objective-C编写的类WeiBo,用于解析从微博API获取的数据。WeiBo类通过初始化字典来设置属性,包括状态更新、广告、可见性等,并在ViewController中实现了异步GET请求以获取数据。
#import <Foundation/Foundation.h>

@interface WeiBo : NSObject
@property (nonatomic,strong) NSArray *statuses;

@property (nonatomic,strong) NSArray *advertises;

@property (nonatomic,strong) NSArray *ad;

@property (nonatomic,strong)NSNumber *hasvisible;

@property (nonatomic,strong)NSNumber *previous_cursor;

@property (nonatomic,strong)NSNumber *next_cursor;

@property (nonatomic,strong)NSNumber *total_number;

@property (nonatomic,strong)NSNumber *interval;

@property (nonatomic,strong)NSNumber *uve_blank;

@property (nonatomic,strong)NSNumber *since_id;

@property (nonatomic,strong)NSNumber *max_id;

@property (nonatomic,strong)NSNumber *has_unread;
-(id)initWithDictionary:(NSDictionary *)dictionary;

@end



#import "WeiBo.h"

@implementation WeiBo
-(id)initWithDictionary:(NSDictionary *)dictionary
{
    if (self = [super init])
    {
        [self setValuesForKeysWithDictionary:dictionary];
    }
    return self;
}
@end

#import "ViewController.h"
#import "WeiBo.h"
@interface ViewController ()<NSURLConnectionDataDelegate>
{
    NSMutableData *mData;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
//异步Get
- (IBAction)asynchronousGet:(id)sender {
    //获取url
    NSString *urlString = @"https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00bUKyMD0Spw_tebfe20df130tHaM_";
    //编码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //转换成NSURL
    NSURL *url = [NSURL URLWithString:urlString];
    //创建并返回一个URL请求,指向一个指定的URL,采用对应的缓存策略 和 超时响应时长(默认时长60秒)
  NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
   NSConnection *connectionGet = [NSURLConnection connectionWithRequest:request delegate:self];

   
}

//服务器开始响应,准备向客户发送信息
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    mData = [NSMutableData data];
}

//服务器接收数据,并且此方法会执行多次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mData appendData:data];
}

//接收数据完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Json数据格式解析,利用系统提供的API进行Json数据解析
   NSDictionary *dictionay = [NSJSONSerialization JSONObjectWithData:mData options:NSJSONReadingAllowFragments error:nil];
    WeiBo *weibo = [[WeiBo alloc]initWithDictionary:dictionay];
    [self.TextView performSelectorOnMainThread:@selector(setText:) withObject:[weibo.statuses[0] objectForKey:@"text"] waitUntilDone:NO];
}

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

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值