代码之家  ›  专栏  ›  技术社区  ›  Mahboob Nur

使用NSURLRequest发送NSMutableDictionary数据的NSMutableArray

  •  0
  • Mahboob Nur  · 技术社区  · 8 年前

    这是我的申请书

    [
        {
        "DealId":677304,
        "CustomerId":328702,
        "CouponQtn":1,
        "PaymentType":"MPD",
        "CustomerMobile":"01670234032",
        "CustomerAlternateMobile":"01670234032",
        "CustomerBillingAddress":"IT test order.......",
        "Sizes":"not selected",
        "Color":"",
        "DeliveryDist":62,
        "CardType":"Manual",
        "OrderFrom":"iOS",
        "MerchantId":14025,
        "OrderSource":"app",
        "AdvPaymentType":0,
        "AdvPayPhoneId":0,
        "deliveryCharge":45,
        "OrderCouponPrice":500
    
        }
    
    ]
    

    我正在尝试使用Restful api中的NSURLRequest发送NSMutableDictionary数据的NSMutableArray,但是我的应用程序遇到了如下异常

    由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[\uUsArraym bytes]:发送到实例0x60000855E40的未识别的选择器”。

    我的解析代码:

        -(NSDictionary*)getDataByPOST:(NSString*)url parameter:(id)parameter{
    
        NSDictionary *dictionaryData;
        NSDictionary *dic;
        Reachability *reachTest = [Reachability reachabilityWithHostName:@"www.apple.com"];
        NetworkStatus internetStatus = [reachTest  currentReachabilityStatus];
        if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)){
            dictionaryData = [[NSDictionary alloc] initWithObjectsAndKeys:@"error",@"status",@"No Network",@"message",nil];
            dic = [NSDictionary dictionaryWithObjectsAndKeys:dictionaryData, @"response",nil];
            return dic;
        }
    
        else{
    
    
            NSURL *s =[self getAbsoluteURL:url];
            NSMutableURLRequest *requestURL = [NSMutableURLRequest requestWithURL:s cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:900.00];
            //NSLog(@"%@",requestURL);
            [requestURL setHTTPMethod:@"POST"];
    
    
            NSError *error=nil ;
            if ([parameter isKindOfClass : [NSString class]]) {
                [requestURL setHTTPBody:[parameter dataUsingEncoding:NSUTF8StringEncoding]];
            }
            else if([parameter isKindOfClass:[NSDictionary class]]) {
                [requestURL setHTTPBody:parameter];
            }
            else {
                [requestURL setHTTPBody:parameter];
            }
            NSHTTPURLResponse *response;
            NSError *error1=nil;
    //        NSLog(@"%@\n\n%@",s,parameter);
            NSData *apiData = [NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error1];
            if (!apiData) {
                NSLog(@"Error: %@", [error localizedDescription]);
                return NO;
            }
    
            if (response.statusCode == 0) {
    
                dictionaryData = [[NSDictionary alloc] initWithObjectsAndKeys:@"error",@"status",@"Server Error",@"message",nil];
                return dic;
    
            }
            else if(response.statusCode == 404) {
                dictionaryData= [[NSDictionary alloc] initWithObjectsAndKeys:@"error",@"status",@"Server is Currently Down.",@"message", nil];
                return dic;
    
            }
            else {
                dictionaryData = [NSJSONSerialization JSONObjectWithData:apiData options:0 error:&error];
            }
        }
        return dictionaryData;
    }
    

    这是我的api调用代码

     tempDic = [apiCom getNodeDataByPOST:CART_ORDER_URL parameter:orderArray];
    

    这里的orderArray是NSMutableArrray包含NSMutableDictionary对象。

    谢谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   Larme    8 年前

    你得到了错误:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM bytes]: unrecognized selector sent to instance 0x600000855e40'.
    

    这意味着在代码中的某个时刻,您认为可以使用 bytes 方法(getter),但实际上该对象是 NSMutableArray 一个,因为它没有实现 字节 ,您的代码因该错误而崩溃现在, 字节 例如 NSData 方法我会回去的。

    作为开发人员,您需要找到导致崩溃的行即使你不理解它,如果你指出了问题,其他人可能会更乐意提供帮助,因为他们可以专注于这个问题,而不会浪费时间去寻找希望找到问题的大部分代码即使不是为了别人,也要为你做。 这不是一个消极的批评,这是一个提示。

    罪魁祸首:

    if ([parameter isKindOfClass : [NSString class]]) {
        [requestURL setHTTPBody:[parameter dataUsingEncoding:NSUTF8StringEncoding]];
    }
    else if([parameter isKindOfClass:[NSDictionary class]]) {
        [requestURL setHTTPBody:parameter];
    }
    else {
        [requestURL setHTTPBody:parameter];
    }
    

    文件 NSMutableURLRequest :

    @property(copy) NSData *HTTPBody;
    

    很明显,如果 parameter 是一个 NSDictionary ,或 NSArray 你会得到同样的崩溃,无论是: -[__NSDictionary bytes]: unrecognized selector sent to instance (或类似的 I 或者 M 在类名中的某个位置 Single 用于优化字典/数组等)

    现在,它取决于Web API的文档: 一个常见的用法是使用JSON:

    else if([parameter isKindOfClass:[NSDictionary class]] || [parameter isKindOfClass:[NSArray class]]) {
        [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameter options:0 error:nil]];
    }
    

    我用过 nil 对于 error ,但如果失败,检查它的值可能会很有趣。

    推荐文章