【发布时间】:2016-03-01 07:15:53
【问题描述】:
您好,我是 iOS 新手,我正在尝试使用 JSON 从 Web 服务获取响应,但出现以下错误。请帮我解决。
Error Domain=NSCocoaErrorDomain Code=3840 "无法执行该操作 完全的。 (Cocoa 错误 3840。)”(JSON 文本不是以数组开头或 允许未设置片段的对象和选项。) UserInfo=0x7fd30bee0f70 {NSDebugDescription=JSON 文本不是以数组或对象开头并且 允许未设置片段的选项。,NSUnderlyingError=0x7fd30bede7b0 “请求失败:内部服务器错误 (500)”}
-(void)loadFeedWithOffset:(NSInteger)Offset Limit:(NSInteger)Limit
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// [manager.requestSerializer setValue:@"application/json; text/html" forHTTPHeaderField:@"Accept"];
// [manager.requestSerializer setValue:@"application/json; text/html; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"UID"] forKey:@"user_id"];
[params setValue:[NSString stringWithFormat:@"%ld",(long)Offset] forKey:@"offset"];
[params setValue:[NSString stringWithFormat:@"%ld",(long)Limit] forKey:@"limit"];
[params setValue:[NSString stringWithFormat:@"%d",[AppDelegate sharedAppDelegate].intPostType] forKey:@"post_type"];
[manager POST:[NSString stringWithFormat:@"%@webservices/post/load", API_URL] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
if ([[responseObject objectForKey:@"status"] isEqualToString:@"fail"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[responseObject objectForKey:@"message"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}
else
{
if ([[responseObject objectForKey:@"feed"] count] > 0)
{
isOver = FALSE;
[arrFeed addObjectsFromArray:[responseObject objectForKey:@"feed"]];
searchedDataArray = [NSMutableArray arrayWithArray:arrFeed];
//searchedDataArray=arrFeed;
[tblMenuDetail reloadData];
}
else
{
isOver = TRUE;
}
[self performSelector:@selector(doneLoadingTableViewData) withObject:self afterDelay:1.0];
}
[[AppDelegate sharedAppDelegate] hideProgress];
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
[[AppDelegate sharedAppDelegate] hideProgress];
NSLog(@"Error: %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}];
}
【问题讨论】:
-
json是什么样子的
-
知道我没有从服务器得到任何响应,所以我不知道响应是哪种格式
-
我更改了上面的代码,但仍然出现错误,错误是 serialization.response Code=-1011 "Request failed: internal server error (500)"
-
错误可能在服务器端。尝试使用邮递员或任何其他应用程序使用您的
params作为参数来模拟您的发布请求。 -
请检查我在下面发布的代码,如果有任何问题,请告诉我。 :)
标签: ios json objective-c web-services cocoa-touch