2012-07-27 2 views
1

AFHTTPClient의 하위 클래스를 만들고 일부 JSON 매개 변수를 서버에 보내려고합니다. 서버가 당신이 AFHTTPClient를 사용하는 경우 AFNetworking FAQAFNetworking http 클라이언트가 JSON 매개 변수를 보내지 않습니다.

에 따르면 예상 콘텐츠 형식

{(
    "text/json", 
    "application/json", 
    "text/javascript" 
)}, got application/xml 

으로 응답 그러나

는 AFJSONParameterEncoding에 parameterEncoding 속성을 설정합니다. 매개 변수 인수가있는 해당 HTTP 클라이언트의 모든 메소드는 전달 된 객체를 JSON 문자열로 인코딩하고 HTTP 본문 및 Content-Type 헤더를 적절하게 설정합니다.

여기까지했는데 서버가 콘텐츠 헤더를 인식하지 못하는 것처럼 보입니다. 누구든지 잠재적 인 해결책을 알고 있습니까?

- (void)getCompanyDataWithString:(NSString*)companySearchQuery 
     finish:(LBMarkitAPIRequestCompletionBlock)finishBlock 
{ 
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
    [self setParameterEncoding:AFJSONParameterEncoding]; 

    NSDictionary *params = [NSDictionary dictionaryWithObject: 
     companySearchQuery forKey:@"input"]; 
    NSMutableURLRequest *searchQueryRequest = [self requestWithMethod:@"GET" 
     path:kMarkitCompanyURL parameters:params]; 

    AFJSONRequestOperation *searchRequestOperation = [AFJSONRequestOperation 
     JSONRequestOperationWithRequest:searchQueryRequest 
     success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) 
     { 
      NSLog(@"Response: %@", response); 
      NSLog(@"JSON: %@",json); 
      NSMutableArray *results = [NSMutableArray array]; 

      NSError *anError = [[NSError alloc] init]; 
      if ([json objectForKey:@"Message"]) 
      { 
       NSString *message = [json objectForKey:@"Message"]; 
       anError = [[NSError alloc] initWithDomain:message 
                code:100 
               userInfo:nil]; 
      } 

      // Need some error handling code here 
      for (id item in json) 
      { 
       NSString *aName = [item objectForKey:@"Name"]; 
       NSString *aSymbol = [item objectForKey:@"Symbol"]; 
       NSString *anExchange = [item objectForKey:@"Exchange"]; 

       LBCompany *aCompany = [[LBCompany alloc] initWithName:aName 
        Symbol:aSymbol Exchange:anExchange]; 
       [results addObject:aCompany]; 
      } 
      // Need to run the passed in block after JSON 
      // Request Operation succeeds 

      finishBlock(results,anError); 
      } 
     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, 
      NSError *error, id JSON) 
     { 
      NSLog(@"request failed: %@",[error localizedDescription]); 
      NSLog(@"Response: %@",response); 
      NSLog(@"JSON: %@",JSON); 
     }]; 

    [searchRequestOperation start]; 
    NSLog(@"JSON operation started"); 
} 

답변

1

문제는 URL 형식으로했다 : 여기

는 방법이다. Query Parameters를 보내고 URI에 JSON 출력을 지정하는 API 구현 세부 사항을 알지 못했습니다.

AFNetworking과 관련된 문제는 없습니다.

+0

안녕하세요, 저는 같은 문제가 있다고 생각하지만 여전히 해결 방법을 찾지 못했습니다. API에 대해 잘못하고있는 부분과 해결 방법에 대해 자세히 알려주십시오. – filo

+0

여기에서 내 솔루션을 확인할 수 있습니다. http://github.com/andrewjl/Aperio/blob/master/Aperio/LBHTTPClient.m –

+0

감사합니다. 덕분에 도움이되었습니다. – filo

관련 문제