iOS网络层业务层-dianping api SDK错误修改

2016-08-25  本文已影响23人  solozyx

1.把dianpingapi文件夹集成到项目中

1-dianpingapi.png

1.1解除对 SBJson 的依赖

2-SBJson.png 3-SBJson.png

把二进制数据data 解析成了 JSON对象,即OC的数组或者字典,

SBJson是很久之前的东西,是解析JSON的,性能差
现在几乎不用了

4-解除对SBJson的依赖.png 5-解除对SBJson的依赖.png
//#import "SBJson.h"


#warning begin 解除对SBJson的依赖
 //SBJsonParser *parser = [[SBJsonParser alloc] init];
    //id result = [parser objectWithData:data];
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data
                                                options:NSJSONReadingAllowFragments
                                                  error:&error];
    NSLog(@"return: \n%@", result);
    if (!result) {
  //NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
  //        parser.error, @"error", nil];
  //NSError *error = [NSError errorWithDomain:kDPAPIErrorDomain
  //          code:-1
  //         userInfo:userInfo];
#warning end 解除对SBJson的依赖
      [self failedWithError:error];
 } else {

使用iOS系统自带的JSON解析

6-2个*使用error的地址.png

error:&error 2个*使用error的地址

1.2解除对应用代理的依赖

6-依赖应用代理.png

大众点评的这个SDK写的不好,还依赖于应用代理,应用代理是非常重要的一个对象,它还在DPAPI的公共接口依赖代理

其他开发者使用这个SDK需要把 DPAppDelegate 拿过去
每个应用只有一个Delegate

7-解除DPAppDelegate依赖.png 8-解除DPAppDelegate依赖.png 9-解除DPAppDelegate依赖.png
//#import "DPAppDelegate.h"


#warning begin 解除对 DPAppDelegate 的依赖
 //NSMutableString *signString = [NSMutableString stringWithString:[[DPAppDelegate instance] appKey]];
 //NSMutableString *paramsString = [NSMutableString stringWithFormat:@"appkey=%@", [[DPAppDelegate instance] appKey]];

    NSMutableString *signString = [NSMutableString stringWithString:kDPAppKey];
    NSMutableString *paramsString = [NSMutableString stringWithFormat:@"appkey=%@", kDPAppKey];
    
 NSArray *sortedKeys = [[paramsDic allKeys] sortedArrayUsingSelector: @selector(compare:)];
 for (NSString *key in sortedKeys) {
  [signString appendFormat:@"%@%@", key, [paramsDic objectForKey:key]];
  [paramsString appendFormat:@"&%@=%@", key, [paramsDic objectForKey:key]];
 }
 //[signString appendString:[[DPAppDelegate instance] appSecret]];
    [signString appendString:kDPAppSecret];
#warning end 解除对 DPAppDelegate 的依赖

1.3测试DPAPI

#import "TESTViewController.h"
#import "DPAPI.h"

@interface TESTViewController () <DPRequestDelegate>
@property (nonatomic,strong) DPAPI *api;
@end

@implementation TESTViewController

- (DPAPI *)api{
    if (_api==nil) {
        _api = [[DPAPI alloc] init];
    }
    return _api;
}

- (void)request:(DPRequest *)request didReceiveResponse:(NSURLResponse *)response{
    
}

- (void)request:(DPRequest *)request didReceiveRawData:(NSData *)data{
    
}

- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result{
    NSLog(@"result = %@",result);
}

- (void)request:(DPRequest *)request didFailWithError:(NSError *)error{
    NSLog(@"error  = %@",error);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"city"] = @"北京";
    [self.api requestWithURL:@"v1/deal/find_deals" params:dict delegate:self];
}

@end
上一篇下一篇

猜你喜欢

热点阅读