【发布时间】:2014-12-31 12:16:05
【问题描述】:
我在网络服务 POST 方法上苦苦挣扎,我在 android 和 iOS 中使用了一项服务。在 android 中它工作正常,但 iOS 响应格式不正确。这里我使用的是 iOS 代码:
Label.text=@"Bangalore";
NSLog(@"text=%@",Label.text);
NSString *myRequestString = [[NSString alloc] initWithFormat:@"city=%@",Label.text];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"http://****************.php"]];
[request setHTTPMethod:@"POST"];
// [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody: myRequestData];
NSURLResponse *response;
NSError *err;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSError* error;
NSMutableArray* result = [NSJSONSerialization JSONObjectWithData:returnData
options:kNilOptions
error:&error];
NSLog(@"sss=%@",result);
我收到这样的回复,
sss={
Bangalore = (
{
Price = 384;
date = "01-12-2014";
},
{
Price = 384;
date = "02-12-2014";
},
{
Price = 384;
date = "03-12-2014";
},
{
Price = 374;
date = "08-12-2014";
},
{
Price = 374;
date = "09-12-2014";
},
{
Price = 365;
date = "13-12-2014";
},
{
Price = 365;
date = "14-12-2014";
},
{
Price = 369;
date = "18-12-2014";
}
);
}
在 android 相同的服务中,我得到了正确的响应,但 iOS 我面临这个问题,请问如何解决?我的代码有任何错误吗?谢谢@
【问题讨论】:
-
你想要的 JSON 响应是什么?
-
从我不想要的最后一个);这个字段。@Naeem
-
我只需要开括号
-
试试 NSDictionary 而不是 NSMutable 数组
-
我也尝试过 NSDictionary,我遇到了同样的问题。
标签: ios objective-c web-services post ios8