2012-09-16 240 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