yue1liang1chuan 2013-08-20 03:07 采纳率: 0%
浏览 2835
已采纳

iPhone应用-JSON请求失败

Json格式如下,要发送请求到服务器,但是返回的是错误500。

{"firstName":"Sharath K", "lastName":"babu", "moMerchantAddresses":[{"email":"abc@abc.co.in"}]} >

谢谢您的帮助。

 NSMutableArray *objects =  [NSMutableArray  arrayWithObjects:@"Sharath",@"babu",@"[{\"email\":\"abc@abc.co.in\"}]", nil];
        NSMutableArray *keys = [NSMutableArray arrayWithObjects:@"firstName",@"lastName",@"moMerchantAddresses", nil];
NSMutableDictionary  *jsonDict = [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict
                                                   options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                     error:nil];

NSString *postLength = [NSString stringWithFormat:@"%d",[jsonData length]];

ServiceInterface *service = [[ServiceInterface alloc] init];
service.theDelegate = self;
service.theSuccessMethod = @selector(responseMerchantCreationService:);
service.theFailureMethod = @selector(requestFailedWithError:);
[self addServiceInterfaceToServiceStack:service];
NSString* stringURL  =  [kBase_URL stringByAppendingString:@"/merchant/create"];

NSString* webStringURL  =  [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url  =  [NSURL URLWithString:webStringURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:jsonData];
[request setTimeoutInterval:30.0f];


NSLog(@"request file :: %@",request);
[service startWithRequest:request];
service = nil;
  • 写回答

1条回答 默认 最新

  • xinyoulingxi1_tong 2013-08-20 07:59
    关注
    NSMutableDictionary *emailDict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [emailDict setObject:@"abc@abc.co.in" forKey:@"email"];
    
    NSMutableArray *emailArr = [ [NSMutableArray alloc] init];
    [emailArr addObject:emailDict];
    
    NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [mainDict setObject:@"Sharath" forKey:@"firstName"];
    [mainDict setObject:@"babu" forKey:@"lastName"];
    [mainDict setObject:emailDict forKey:@"moMerchantAddresses"];
    

    修改mainDict为:

    NSData *jsonData = [ NSJSONSerialization dataWithJSONObject:mainDict options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                             error:nil];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障