2012-09-16 191 views
14

我在从AFNetworking发出POST请求时收到error.userInfo中的此响应。 任何人都可以告诉我失去了什么明显的东西或需要修复我的服务器端?AFNetworking和POST请求

Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {( "text/json", "application/json", "text/javascript")}, got text/html" UserInfo=0x6d7a730 {NSLocalizedRecoverySuggestion=index test, AFNetworkingOperationFailingURLResponseErrorKey=, NSErrorFailingURLKey=http://54.245.14.201/, NSLocalizedDescription=Expected content type {( "text/json", "application/json", "text/javascript")}, got text/html, AFNetworkingOperationFailingURLRequestErrorKey=http://54.245.14.201/>}, { AFNetworkingOperationFailingURLRequestErrorKey = "http://54.245.14.201/>"; AFNetworkingOperationFailingURLResponseErrorKey = ""; NSErrorFailingURLKey = "http://54.245.14.201/"; NSLocalizedDescription = "Expected content type {(\n \"text/json\",\n \"application/json\",\n
\"text/javascript\"\n)}, got 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]; 
+0

一问,为什么你有“[operation waitUntilFinished];”?这是否需要,是否阻止了呼叫?谢谢! :) – trillions

+1

所以......在上面的代码中,您是如何实施公认的解决方案的? – Morkrom

回答

46

默认情况下,AFJSONRequestOperation只接受“文字/ JSON” ,“application/json”或者“text/javascript”内容类型,但是你会得到“text/html”。

固定在服务器上会更好,但你也可以添加“text/html的”内容键入您的应用程序可以接受的:

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

它的工作对我来说,希望这有助于!

4

你发送的AFHTTPClient这个POST请求?如果是这样,你需要设置操作类是:

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]); 
      // ... 
     } 
+0

是的;我正在使用AFHTTPClient,并设置了提到的类;请查看我的代码,我只是编辑了我的问题; – Ans

+0

@Ans AFHTTPClient有一个'-postPath:parameters:success:'方法。你试过了吗? – Kjuly

+0

感谢您的回复;我试过你的代码,但现在既没有成功也没有执行失败块; – Ans

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]; 
+0

这里kBASEURL想要http://xyz.com/api和groups /是api终点正确??嗯..我尝试了这一点,但当它发送电话,什么也没有发生;不知道实际发生的事情;即使我的网址不正确的形式,altest扔等错误块等错误; – Ans

+0

验证完整的网址 – yunas

+0

我把我的基地网址代替kBASEURL,并把我的api终结点替换你的团体/是否可以吗? – Ans