2012-04-07 88 views
0
NSURL *url = [NSURL URLWithString:@"contact.php"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
         @"companyID", constCompanyID, 
         nil]; 

NSURLRequest *request = [httpClient requestWithMethod: @"POST" path:@"contact.php" parameters:params]; 


AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    for(id entry in JSON) 
    { 
     self.strAddr = [entry valueForKeyPath:@"addr"]; 
     CCLOG(@"Address: %@", self.strAddr); 
    } 
} failure:nil]; 
[operation start]; 
CCLOG(@"Address: %@", strAddr); 

在日誌中,地址爲空。 PHP很好;在瀏覽器中,它返回我想要的。AFJsonRequest和AFHttpClient一起工作嗎?

此代碼一直工作,直到我添加了AFHTTPClient來傳遞變量。我一定在做錯事,或者我不能這樣做?

回答

2

在第1行中,當您真正需要的是基本URL時,您嘗試通過「contact.php」(其中NSURL +URLWithString:返回nil)創建一個URL--完整的URL,特定的Web服務,例如「http://api.twitter.com/1/」。