在 Objective C 中使用 NSURLConnection 解析 json 时获取服务器错误消息

Getting server error message while parsing json using NSURLConnection in Objective C

[
{
    "CouponQtn":1,
    "DeliveryDist":14,
    "Sizes":"not selected",
    "CustomerAlternateMobile":"01700790900",
    "deliveryCharge":0,
    "DealId":744706,
    "PaymentType":"MPD",
    "OrderFrom":"ios",
    "CardType":"Manual",
    "OrderSource":"app",
    "CustomerId":630142,
    "MerchantId":15196,
    "AdvPaymentType":0,
    "CustomerBillingAddress":"Khulna",
    "CustomerMobile":"01700790900",
    "Color":"",
    "AdvPayPhoneId":0,
    "OrderCouponPrice":375

}
]

这是我从应用程序填充的 json 格式,我用 post 方法将其发送到服务器以获得所需的 json 结果。但是服务器向我提供了一个未知错误。但它也适用于 Postman 在这里,我将字典数组转换为 json

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:orderArray
                                                                                   options:kNilOptions
                                                                                     error:nil];
                                NSString*jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

                               //NSString* jsonStr = [NSString stringWithUTF8String:[jsonData bytes]];


                                tempDic = [apiCom getNodeDataByPOST:CART_ORDER_URL parameter:[NSString stringWithFormat:@"%@",jsonStr]];

这里的orderArray是我的字典数组,下面提供了我的解析代码

 -(NSDictionary*)getNodeDataByPOST:(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 *adUrl =[NSURL URLWithString:url];

        NSMutableURLRequest *requestURL = [NSMutableURLRequest requestWithURL:adUrl 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 if([parameter isKindOfClass:[NSArray class]]||[parameter isKindOfClass:[NSDictionary class]]) {
            [requestURL setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameter options:0 error:nil]];

        }
        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]);

        }

        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:kNilOptions error:&error];


        }
    }
    return dictionaryData;
}

我没有发现任何编码或逻辑错误。我已经多次使用此代码并且运行良好。应用端是否有任何设置需要权限?

谢谢....

你说它在 Postman 中工作正常,你应该检查请求是否以 form-url 编码格式发送。如果是,您应该添加以下行

       [requestURL setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

我用这条线对我来说就像魔法一样

 [requestURL setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];