现在的位置: 首页 > 综合 > 正文

ios 网络请求参数为json

2013年02月14日 ⁄ 综合 ⁄ 共 2599字 ⁄ 字号 评论关闭

#import "CKRequestShopping.h"

@implementation CKRequestShopping

- (void)shoppingWithUsername:(NSString *)username

                    password:(NSString *)password

                  goodsArray:(NSArray *)array

{

    

    NSMutableArray *mArray = [NSMutableArray
new];

   
for (CKModelGoods *good
in array)

    {

        NSMutableDictionary *dictionary = [NSMutableDictionary
new];

        [dictionary
setValue:good.productID
forKey:@"goodsid"];

       
if (good.quality ==
0)

        {

            good.quality =
1;

        }

        [dictionary
setValue:[NSString
stringWithFormat:@"%d",good.quality]
forKey:@"count"];

        [mArray
addObject:dictionary];

    }

    NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:mArray options:NSJSONWritingPrettyPrinted
error:nil];

    NSString *jsonString = [[NSString
alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];

    

   
NSLog(@"%@",jsonString);

    

    

   
NSDictionary *dict =
@{
@"username":username,@"pw":password,@"goodsarr":jsonString};

    

    [self
startRequestWithStrUrl:URL_SHOPPING
postKeysAndValues:dict];

}

//1 购买礼物成功

//-1:用户不存在,或者被删除

//-2:密码错

//-4可用余额不足

//-5商品不存或者下架

//-6数据交互失败

//-10 缺少参数

- (NSString *)handleErrorCode:(NSString *)str

{

   

   
NSString *strError;

   
switch (str.intValue)

    {

       
case -1:

            strError =
@"用户不存在,或者被删除";

           
break;

            

       
case -2:

            strError =
@"密码错";

           
break;

            

       
case -4:

            strError =
@"可用余额不足";

           
break;

            

       
case -5:

            strError =
@"商品不存在或者已下架";

           
break;

            

       
case -6:

            strError =
@"数据交互失败!";

           
break;

            

       
case -10:

            strError =
@"缺少参数";

           
break;

            

       
default:

            strError =
@"未知错误";

           
break;

    }

   
return strError;

}

- (void)onRequestDidFinish:(ASIHTTPRequest *)request

{

    [super
onRequestDidFinish:request];

   
NSMutableArray *array =
nil
;

    NSLog(@"%@",_jsonDict[@"succ"]);

    

   
if ([_jsonDict[@"succ"]
intValue] == 1)

    {

//        [CKUtility showSimpleAlertWithString:@"购买成功"];

        [[NSNotificationCenter
defaultCenter] postNotificationName:NOTI_SHOPPING_SUCCESS
object:_jsonDict[@"succ"]];

    }

   
else

    {

       
if ([_jsonDict[@"succ"]
intValue] == -5) {

            

            array =
_jsonDict[@"data"];

            [[NSNotificationCenter
defaultCenter] postNotificationName:NOTI_SHOPPING_FAIL
object:array];

        }

       
else

        {

            [[NSNotificationCenter
defaultCenter] postNotificationName:NOTI_SHOPPING_FAIL
object:[self
handleErrorCode:_jsonDict[@"succ"]]];

        }

//        [CKUtility showSimpleAlertWithString:[self handleErrorCode:_jsonDict[@"succ"]]];

//        [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_SHOPPING_FAIL object:[self handleErrorCode:_jsonDict[@"succ"]]];

    }

}

- (void)onRequestDidFail:(ASIHTTPRequest *)request

{

    [super
onRequestDidFail:request];

    [[NSNotificationCenter
defaultCenter] postNotificationName:NOTI_SHOPPING_FAIL
object:@"网络请求失败"];

}

@end


抱歉!评论已关闭.