• [iOS微博项目


     
    A.使用微博API发送微博
    1.需求
    学习发送微博API
    发送文字微博
    发送带有图片的微博
     
    2.思路
    直接按照微博API的文档指示使用
    Image(120)
     
    这里测试上传带图片微博
    Image(121)
     
    3.实现
    在“发微博”界面,点击右上角发送就调用API
     
    Image(122)
     
     1 //  HVWComposeViewController.m
     2 /** 发送微博 */
     3 - (void) sendWeibo {
     4     if (self.composeView.text.length == 0) {
     5         [MBProgressHUD showError:@"你好像忘记了内容..."];
     6         return;
     7     }
     8    
     9     [MBProgressHUD showMessage:@"发送微博中..."];
    10    
    11     if (self.imageDisplayView.images.count) { // 发送的时带图片的微博
    12         [self sendWeiboWithTextNImage];
    13     } else { // 发送的是纯文字微博
    14         [self sendWeiboWithText];
    15     }
    16 }
    17 
    18 /** 发送文字微博 */
    19 - (void) sendWeiboWithText {
    20     // 创建http操作管理者
    21     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    22    
    23     // 设置参数
    24     NSMutableDictionary *param = [NSMutableDictionary dictionary];
    25     // 访问令牌
    26     HVWAccountInfo *accountInfo = [HVWAccountInfoTool accountInfo];
    27     param[@"access_token"] = accountInfo.access_token;
    28     // 微博文本
    29     param[@"status"] = self.composeView.text;
    30    
    31     // 发送请求
    32     [manager POST:@"https://api.weibo.com/2/statuses/update.json" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
    33         [MBProgressHUD hideHUD];
    34         [MBProgressHUD showSuccess:@"发送成功!"];
    35     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    36         HVWLog(@"发送微博失败, error:%@", error);
    37         [MBProgressHUD hideHUD];
    38         [MBProgressHUD showError:@"发送失败!error"];
    39     }];
    40 }
    41 
    42 /** 发送图文微博 */
    43 - (void) sendWeiboWithTextNImage {
    44     if (self.imageDisplayView.images.count == 0) {
    45         [MBProgressHUD hideHUD];
    46         [MBProgressHUD showError:@"懵了,找不到图儿!"];
    47         return;
    48     }
    49    
    50     // 创建http操作管理者
    51     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    52    
    53     // 设置参数
    54     NSMutableDictionary *param = [NSMutableDictionary dictionary];
    55     // 访问令牌
    56     HVWAccountInfo *accountInfo = [HVWAccountInfoTool accountInfo];
    57     param[@"access_token"] = accountInfo.access_token;
    58     // 微博文本
    59     param[@"status"] = self.composeView.text;
    60    
    61     // 发送请求
    62     [manager POST:@"https://upload.api.weibo.com/2/statuses/upload.json" parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    63        
    64         // 发送的图片数据,其实现在开放的API只允许上传一张图片
    65         UIImage *image  = [self.imageDisplayView.images firstObject];
    66         if (image) {
    67             NSData *imageData = UIImagePNGRepresentation(image);
    68             [formData appendPartWithFileData:imageData name:@"pic" fileName:@"statusPic" mimeType:@"image/png"];
    69         }
    70     } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    71         [MBProgressHUD hideHUD];
    72         [MBProgressHUD showSuccess:@"发送成功!"];
    73     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    74         HVWLog(@"发送微博失败, error:%@", error);
    75         [MBProgressHUD hideHUD];
    76         [MBProgressHUD showError:@"发送失败!"];
    77     }];
    78 }
     
    发送成功:
    Image(123)
     
     
    #mark:
    发送微博失败的原因有:
    1.没有文本内容
    2.使用发送纯文本API,却上传图片
    3.使用发送图文API,却没有上传图片
     
    注意:新浪开放的API一次只能上传一张图片,选择多张的时候会使用最后一张
     
  • 相关阅读:
    网络基础 HTTP协议之http url简介
    MySql 简单统计查询消耗时间脚本
    Java java jdk在Linux下安装与环境变量的配置
    MySql 正则表达式简介及使用
    MySql UNIX_TIMESTAMP和FROM_UNIXTIME函数讲解
    MySql 定时任务的使用
    MySql 利用crontab实现MySql定时任务
    MySql 利用mysql&mysqldum导入导出数据
    排错-windows平台下访问oracle em出现空白的解决方法
    Oracle 修改oracle数据库名
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4280739.html
Copyright © 2020-2023  润新知