AFNNetworking2.0版本的使用

作者: shaneZhang 分类: ios技术 发布时间: 2014-05-02 16:25

虽然是2.0版本了,但是使用的方法还是老的方法了,请求网络也能够成功,符合我的程序规范就Ok了

好了,废话不多说,直接上代码

 NSString *urlString = @"http://www.XXXXX.com/getmessage.php";
   __block NSMutableArray *result;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    [dictM setObject:@"json" forKey:@"format"];
    [manager GET:urlString parameters:dictM success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
        NSLog(@"%@",responseObject);
         if (result == nil)
         {
             result = [NSMutableArray array];
         }
         NSArray *array = responseObject;
         for (NSDictionary *dict in array)
         {
             PublicMessage *publicMessage = [[PublicMessage alloc]init];
             publicMessage.ID = [[dict objectForKey:@"id"]integerValue];
             publicMessage.username = [dict objectForKey:@"username"];
             publicMessage.title = [dict objectForKey:@"title"];
             publicMessage.content = [dict objectForKey:@"content"];
             publicMessage.good = [dict objectForKey:@"good"];
             publicMessage.bad = [dict objectForKey:@"bad"];
             publicMessage.time = [[dict objectForKey:@"time"]integerValue];
             PublicMessage *public = [[DBManager sharedDBManager]findPublicMessageById:publicMessage.ID];
             if (public == nil)
             {
                 // 说明没有插入到数据库的数据
                 [[DBManager sharedDBManager]createPublicMessage:publicMessage];
             }
             else
             {
                 // 更新该条的共有数据信息
                 [[DBManager sharedDBManager]updatePublicMessageWithPublicMessage:publicMessage];
             }
         }
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        NSLog(@"请求网络失败=---%@",error.localizedDescription);
        
    }];
    

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注