#import <Foundation/Foundation.h>
@interface WeiBoModle : 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 "WeiBoModle.h"
@implementation WeiBoModle
-(id)initWithDictionary:(NSDictionary *)dictionary
{
if (self = [super init])
{
[self setValuesForKeysWithDictionary:dictionary];
}
return self;
}
@end
#import "ViewController.h"
#import "WeiBoModle.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//同步get
- (IBAction)synchronousGet:(id)sender {
//拿到url
NSString *urlString = @"https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00bUKyMDhwQ3UD584ae943a1b3KGwC";
//编码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//转换成NSURl
NSURL *url = [NSURL URLWithString:urlString];
// 创建并返回一个URL请求,指向一个指定的URl,采用对应的缓存策略 和 超市相应时长
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
//在oc中使用 NSURLConnection类,和服务器连接,这里就是同步连接
NSURLResponse *httpUrlResponse;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&httpUrlResponse error:&error];
//Json数据格式解析,利用系统提供的API进行Json数据解析
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
WeiBoModle *weiBo = [[WeiBoModle alloc]initWithDictionary:dictionary];
self.TextView.text = [weiBo.statuses[0] objectForKey:@"text"];
}