微博的开发

来源:互联网 发布:热血江湖传源码 编辑:程序博客网 时间:2024/05/16 14:45

import “ViewController.h”

import “WeiboInfoModel.h”

@interface ViewController (){
NSMutableData *mdata;

NSURLConnection *getAsyncCon;NSURLConnection *postAsyncCon;

}
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }
  • (IBAction)getSyncAction:(UIButton *)sender {
    //拿到UrlString
    NSString *urlString = @”https://api.weibo.com/2/statuses/home_timeline.json?access_token=你自己的token值”;
    //将urlString进行编码
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:4];
    //转换成NSURL
    NSURL *url = [NSURL URLWithString:urlString];

    //在OC中使用NSURLRequest类,向服务器发送请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];

    //在OC中使用NSURLConnection类,和服务器连接,这里是同步连接过程
    NSHTTPURLResponse *httpUrlResponse;
    NSError *error;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&httpUrlResponse error:&error];

    // NSString *dataString = [[NSString alloc] initWithData:data encoding:4];
    // NSLog(@”dataString = %@”,dataString);
    //Json数据格式解析方式有很多种,如使用 JsonKit,这里用系统提供的API
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

    WeiboInfoModel *weiboInfoModel = [[WeiboInfoModel alloc] initWithDictionary:dictionary];
    NSLog(@”第一条微博信息:%@”,weiboInfoModel.statuses[0]);
    self.textView.text = weiboInfoModel.statuses[0][@”text”];
    }

  • (IBAction)getAsyncAction:(UIButton *)sender {

    //拿到UrlString
    NSString *urlString = @”https://api.weibo.com/2/statuses/home_timeline.json?access_token=你自己的token值”;
    //将urlString进行编码
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:4];
    //转换成NSURL
    NSURL *url = [NSURL URLWithString:urlString];

    //在OC中使用NSURLRequest类,向服务器发送请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
    //异步链接
    getAsyncCon = [NSURLConnection connectionWithRequest:request delegate:self];

}
- (IBAction)postSyncAction:(id)sender {
/*
//拿到UrlString
NSString *urlString = @”https://api.weibo.com/2/statuses/update.json“;
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:4];
NSURL *url = [NSURL URLWithString:urlString];

 NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f]; [mRequest setHTTPMethod:@"POST"]; [mRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; //设置提交的内容 NSMutableDictionary *postDic = [NSMutableDictionary dictionary]; NSString *publishText = @"学生太笨了,老师好累啊..."; publishText = [publishText stringByAddingPercentEscapesUsingEncoding:4]; [postDic setValue:publishText forKey:@"status"]; [postDic setValue:@3 forKey:@"visible"]; [postDic setValue:@0 forKey:@"list_id"]; //将字典转换成data NSData *postData = [NSJSONSerialization dataWithJSONObject:postDic options:NSJSONWritingPrettyPrinted error:nil]; [mRequest setHTTPBody:postData]; [mRequest setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"]; NSData *resultData = [NSURLConnection sendSynchronousRequest:mRequest returningResponse:nil error:nil]; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingAllowFragments error:nil]; NSLog(@"dic = %@",dic); *///拿到UrlStringNSString *urlString = @"https://api.weibo.com/2/statuses/update.json?access_token=你自己的token值&status=学生太笨了,老师好累啊...";urlString = [urlString stringByAddingPercentEscapesUsingEncoding:4];NSURL *url = [NSURL URLWithString:urlString];NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f];[mRequest setHTTPMethod:@"POST"];[mRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];//设置提交的内容NSMutableDictionary *postDic = [NSMutableDictionary dictionary];NSString *publishText = @"学生太笨了,老师好累啊...";publishText = [publishText stringByAddingPercentEscapesUsingEncoding:4];[postDic setValue:publishText forKey:@"status"];[postDic setValue:@3 forKey:@"visible"];[postDic setValue:@0 forKey:@"list_id"];//将字典转换成dataNSData *postData = [NSJSONSerialization dataWithJSONObject:postDic options:NSJSONWritingPrettyPrinted error:nil];[mRequest setHTTPBody:postData];[mRequest setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"];NSData *resultData = [NSURLConnection sendSynchronousRequest:mRequest returningResponse:nil error:nil];NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingAllowFragments error:nil];NSLog(@"dic = %@",dic);

}
- (IBAction)postAsyncAction:(UIButton *)sender {
//拿到UrlString
NSString *urlString = @”https://api.weibo.com/2/statuses/update.json?access_token=你自己的token值&status=学生太笨了,老师好累啊…”;
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:4];
NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f];[mRequest setHTTPMethod:@"POST"];[mRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];//设置提交的内容NSMutableDictionary *postDic = [NSMutableDictionary dictionary];NSString *publishText = @"学生太笨了,老师好累啊...";publishText = [publishText stringByAddingPercentEscapesUsingEncoding:4];[postDic setValue:publishText forKey:@"status"];[postDic setValue:@3 forKey:@"visible"];[postDic setValue:@0 forKey:@"list_id"];//将字典转换成dataNSData *postData = [NSJSONSerialization dataWithJSONObject:postDic options:NSJSONWritingPrettyPrinted error:nil];[mRequest setHTTPBody:postData];[mRequest setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"];postAsyncCon = [NSURLConnection connectionWithRequest:mRequest delegate:self];

}

pragma mark ————

//服务器开始响应,准备向客户端发送数据
- (void)connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse )response{
if (connection == getAsyncCon) {
mdata = [[NSMutableData alloc] init];
}

if (connection == postAsyncCon) {    mdata = [[NSMutableData alloc] init];}

}
//从服务器端接收数据,且此方法会执行多次
- (void)connection:(NSURLConnection )connection didReceiveData:(NSData )data{
if (connection == getAsyncCon) {
[mdata appendData:data];
}

if (connection == postAsyncCon) {    [mdata appendData:data];}

}
//接受数据完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection == getAsyncCon) {
//Json数据格式解析方式有很多种,如使用 JsonKit,这里用系统提供的API
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:mdata options:NSJSONReadingAllowFragments error:nil];

    WeiboInfoModel *weiboInfoModel = [[WeiboInfoModel alloc] initWithDictionary:dictionary];    //次代理方法是在子线程中执行,更新UI时,需返回主线程    [self.textView performSelectorOnMainThread:@selector(setText:) withObject:weiboInfoModel.statuses[0][@"text"] waitUntilDone:NO];}if (connection == postAsyncCon) {    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:mdata options:NSJSONReadingAllowFragments error:nil];    NSLog(@"dic = %@",dic);}

}
//接收数据失败
-(void)connection:(NSURLConnection )connection didFailWithError:(NSError )error{
NSLog(@”error = %@”,error.debugDescription);
}

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

@end

0 0
原创粉丝点击