14

我从 error.userInfo 发出 POST 请求时收到此响应AFNetworking。谁能告诉我缺少任何明显的东西或需要在我的服务器端修复的东西?

请求失败并出现错误:错误域 = AFNetworkingErrorDomain 代码 =-1016 “预期的内容类型 {(“text/json”、“application/json”、“text/javascript”)},得到了 text/html”UserInfo=0x6d7a730 {NSLocalizedRecoverySuggestion=索引测试,AFNetworkingOperationFailingURLResponseErrorKey=,NSErrorFailingURLKey=http://54.245.14.201/,NSLocalizedDescription=预期内容类型{(“text/json”,“application/json”,“text/javascript”)},得到文本/html,AFNetworkingOperationFailingURLRequestErrorKey =http://54.245.14.201/>}, { AFNetworkingOperationFailingURLRequestErrorKey = "http://54.245.14.201/>"; AFNetworkingOperationFailingURLResponseErrorKey = ""; NSErrorFailingURLKey = "http://54.245.14.201/" ; NSLocalizedDescription = "预期的内容类型 {(\n \"text/json\",\n \"application/json\",\n
\"text/javascript\"\n)}, 得到了 text/html"; NSLocalizedRecoverySuggestion = "index test"; }

我正在使用这段代码;

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"Ans", @"name",
                        @"29", @"age",
                        nil];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/" parameters:params];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"Success");
        NSLog(@"%@",JSON);

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
        NSLog(@"Failure");
}];

[operation start];
[operation waitUntilFinished];
4

3 回答 3

46

默认情况下,AFJSONRequestOperation仅接受来自服务器的“text/json”、“application/json”或“text/javascript”内容类型,但您得到的是“text/html”。

在服务器上修复会更好,但您也可以在您的应用程序中添加“text/html”内容类型作为可接受的内容:

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

它对我有用,希望这有帮助!

于 2012-10-04T12:01:14.200 回答
4

您是否发送了此 POST 请求AFHTTPClient?如果是这样,您需要为其设置操作类:

AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8080"]];
// ...
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
// ...

// EDIT: Use AFHTTPClient's POST method
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"Ans", @"name",
                         @"29",  @"age", nil];

// POST, and for GET request, you need to use |-getPath:parameters:success:failure:|
[client postPath:@"/"
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
           NSLog(@"RESPONSE: %@", responseObject);
           // ...
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           if (error)
             NSLog(@"%@", [error localizedDescription]);
           // ...
         }
于 2012-09-16T14:51:21.373 回答
0

在此代码中设置您的值并检查它是否适合您

 AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:kBASEURL]];
        NSString *_path = [NSString stringWithFormat:@"groups/"];
        _path = [_path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%s %@",__PRETTY_FUNCTION__,_path);
        NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
                                                                path:_path
                                                          parameters:postParams];
        [httpClient release];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation 
                                             JSONRequestOperationWithRequest:request
                                             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                                 if ([JSON isKindOfClass:[NSArray class]] || [JSON isKindOfClass:[NSDictionary class]]) {
                                                     completed(JSON);
                                                 }
                                                 else {
                                                 }
                                                 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];                                             

                                             } 
                                             failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                 NSLog(@" response %@  \n error %@ \n JSON %@",response,error,JSON);
                                                 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];                                         
                                                 errored(error);
                                             }];

        NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
        [queue addOperation:operation];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];   
于 2012-09-16T15:29:08.607 回答