2012-09-16 57 views
14

Tôi nhận được phản hồi này do nhầm lẫn.userInfo khi thực hiện yêu cầu POST từ AFNetworking. Bất cứ ai có thể nói hoặc là tôi thiếu bất cứ điều gì rõ ràng hoặc một cái gì đó cần phải sửa chữa ở cuối máy chủ của tôi?Yêu cầu AFNetworking và 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"; }

Và tôi đang sử dụng mã này;

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

một yêu cầu, tại sao bạn có "[operation waitUntilFinished];"? Đây có phải là cần thiết và nó là một cuộc gọi chặn? Cảm ơn! :) – trillions

+1

vì vậy ... ở đâu/như thế nào trong mã trên bạn đã thực hiện các giải pháp được chấp nhận? – Morkrom

Trả lời

46

Theo mặc định, AFJSONRequestOperation chỉ chấp nhận "text/json" , "application/json" hoặc "text/javascript" nội dung các loại từ máy chủ, nhưng bạn đang nhận được "văn bản/html".

Sửa trên máy chủ sẽ tốt hơn, nhưng bạn cũng có thể thêm "text/html" nội dung gõ như chấp nhận được trong ứng dụng của bạn:

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

Nó làm việc cho tôi, hy vọng điều này sẽ giúp!

4

Bạn có gửi yêu cầu POST này bằng AFHTTPClient không? Nếu vậy, bạn cần phải thiết lập lớp hoạt động cho nó:

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

Có; Tôi đang sử dụng AFHTTPClient và cũng đã đề cập đến lớp học; Hãy nhìn vào mã của tôi, tôi chỉ chỉnh sửa câu hỏi của tôi; – Ans

+0

@ Có phương thức '-postPath: parameters: success:' cho AFHTTPClient. Bạn đã thử chưa? – Kjuly

+0

Cảm ơn bạn đã trả lời; Tôi đã thử mã của bạn nhưng bây giờ cả thành công lẫn khối lỗi đều không được thực thi; – Ans

0

Đặt giá trị của bạn trong mã này và kiểm tra nếu nó làm việc cho bạn

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

tại đây kBASEURL muốn http://xyz.com/api và các nhóm/là api cuối cùng điểm phải không? umm .. tôi cũng đã cố gắng này, nhưng khi nó được gửi cuộc gọi, không có gì xảy ra sau đó, không chắc chắn những gì thực sự xảy ra, ngay cả khi url của tôi không ở dạng đúng, altest ném một erro vv trong khối thất bại vv; – Ans

+0

xác minh url hoàn chỉnh – yunas

+0

Tôi đặt url cơ sở của tôi thay cho kBASEURL và đặt thay thế nhóm của bạn/với điểm kết thúc api của tôi; Có phải không? – Ans

Các vấn đề liên quan