我正在使用此网址“http://test.ccsplinfo.com/services.svc/addSubscriber?userdetails={Email_Id:mishra.bn@gmail.com,FirstName:BN,LastName:MISHRA,MobileNumber:8285547554,Password:mypass}”从服务器获取数据。但我没有得到任何回应。当我只是复制和粘贴
这个URL到浏览器然后得到真正的response.it意味着url工作正常,但我在我的代码中得到错误我的代码在这里..
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"mishra.bn@gmail.com" forKey:@"Email_Id"];
[dict setObject:@"BN" forKey:@"FirstName"];
[dict setObject:@"MISHRA" forKey:@"LastName"];
[dict setObject:@"8285547554" forKey:@"MobileNumber"];
[dict setObject:@"mypass" forKey:@"Password"];
NSString *PostUrl=@"http://test.ccsplinfo.com/services.svc/addSubscriber?userdetails=";
NSError *serr;
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&serr];
if (serr)
{
NSLog(@"Error generating json data for send dictionary...");
NSLog(@"Error (%@), error: %@", dict, serr);
return;
}
// Create request object
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:PostUrl]];
request.HTTPMethod = @"POST";
request.HTTPBody = jsonData;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:
[NSString stringWithFormat:@"%lu",
(unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *r, NSData *data, NSError *error)
{
if (!data)
{
NSLog(@"No data returned from server, error ocurred: %@", error);
NSString *userErrorText = [NSString stringWithFormat:
@"Error communicating with server: %@", error.localizedDescription];
return;
}
NSError *deserr;
NSDictionary *responseDict = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&deserr];
NSLog(@" responseDict :%@", responseDict);
}];
Thanks
答案 0 :(得分:0)
您的要求看起来很好。我想你会低于错误
Error Domain=NSCocoaErrorDomain Code=3840
JSON text did not start with array or object and option to allow fragments not set.
这是因为服务器使用无效的JSON对象进行响应。