微博的开发
来源:互联网 发布:热血江湖传源码 编辑:程序博客网 时间: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
- 微博的开发
- 微博开发的一些资源
- 网易微博的Oauth认证开发
- 基于SAE的微博应用开发
- 腾讯微博的开发过程
- 一般微信的开发
- 微信开发的CreateTime
- 微信开发的基本知识
- 微信开发的资料
- 新浪微博开发之微博主页的实现
- 新浪微博开发之php开发
- java视频教程-新浪微博开发的第二讲
- 新浪微博应用开发的又一个解决方案
- 新浪微博开发之授权的实现(一)
- 新浪微博开发之授权的实现(二)
- 新浪微博开发之主程序界面的实现
- 新浪微博开发之授权用户登录的实现
- 在Android使用新浪微博的开发平台API
- POJ 2075 Tangled in Cables 最小生成树 Kruskal && Prim
- iOS 开发小技巧
- hdu5238
- HDU 4508.湫湫系列故事——减肥记I【背包】【算法的优化】【8月13】
- java dom解析xml
- 微博的开发
- jquery记忆笔记
- pat 1096
- Windows下串口逻辑端口号大于10无法打开问题
- MySQL复制表数据到新表的方法
- git revert和git reset的区别
- 关于在win 32位编译的代码在64位执行不了的原因
- Hadoop相关基础知识
- 关于自定义UITableViewCell布局点击Cell消失的问题