我正在通过网络服务发送帖子请求,但我没有收到我想要的回复。
这是我的代码:
NSString *newurlString = [NSString stringWithFormat:@"{\"name\":\"asit\",\"email\":\"m.kulkarni@easternenterprise.com\",\"businessType\":\"1\",\"score\":30}"];
NSString * url = @"http://www.nieuwe-dag.nl/mobile_be/public/?action=saveScore";
//NSString *urlString =[NSString stringWithFormat:@"name=%@&email=%@&businessType=%d&score=%d",name, email, bussinesstype, score];
NSData *postData = [newurlString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"urlString::%@",newurlString);
NSLog(@"postLength::%@",postLength);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:300];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection){
webData = [[NSMutableData data] retain];
}
else {
NSLog(@"theConnection is NULL");
}
答案 0 :(得分:0)
您正在将请求类型设置为json,但您没有创建json。您正在创建的数据只是nsstring。我认为你需要创建一个json对象。您可以从此处下载JSON API:
https://github.com/stig/json-framework/
使用[SBJSONWriter dataWithObject:]创建JSON对象。然后将其传递给您的请求。
有关JSON的更多信息:
答案 1 :(得分:0)
您需要正确的 postData
NSDictionary *dataDict = @{
@"name": @"asit",
@"email": @"m.kulkarni@easternenterprise.com",
@"businessType": @"1",
@"score": @(30)
};
NSData *postData = [NSJSONSerialization dataWithJSONObject:dataDict options:0 error:nil];