2009-11-24 39 views
0

我有一個iphone應用程序,我試圖使用HTTPRiot做一些API調用Web應用程序。問題是我看不到任何HTTPRiot委託方法正在被調用。我已經登錄了所有的委託方法,並且還在查看Web服務器日誌。我看到網址正在被擊中。問題得到HTTPRiot工作

//API.h 
#import <Foundation/Foundation.h> 
#include <HTTPRiot/HTTPRiot.h> 


@interface API : HRRestModel { 

} 

+(void)runTest; 

@end 



//API.m 
#import "API.h" 


@implementation API 


+ (void)initialize { 

    NSLog(@"api initialize"); 

    [self setDelegate:self]; 
    [self setBaseURL:[NSURL URLWithString:@"http://localhost:3000/api"]]; 
    [self setBasicAuthWithUsername:@"demo" password:@"123456"]; 

    NSDictionary *params = [NSDictionary dictionaryWithObject:@"1234567" forKey:@"api_key"]; 
    [self setDefaultParams:params]; 


}//end initialize 


+(void)runTest { 

    NSLog(@"api run test"); 

    // Would send a request to http://localhost:1234/api/people/1?api_key=1234567 
    [self getPath:@"/save_diet" withOptions:nil object:nil]; 
} 


+(void)restConnection:(NSURLConnection *)connection didReturnResource:(id)resource object:(id)object { 
    NSLog(@"didReturnResource"); 
} 



+(void)restConnection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response object:(id)object { 
    NSLog(@"didReceiveResponse"); 
} 



+(void)restConnection:(NSURLConnection *)connection didReceiveParseError:(NSError *)error responseBody:(NSString *)body object:(id)object { 
    NSLog(@"didReceiveParseError"); 
} 



+(void)restConnection:(NSURLConnection *)connection didReceiveError:(NSError *)error response:(NSHTTPURLResponse *)response object:(id)object { 
    NSLog(@"didReceiveError"); 
} 



+(void)restConnection:(NSURLConnection *)connection didFailWithError:(NSError *)error object:(id)object { 
    NSLog(@"didFailWithError"); 
} 


@end 


//test code 
[API runTest]; 


//log output 

回答

0

兩兩件事:

1-- 你使用類方法是錯誤的。 +初始化是一種保證被運行時調用一次的方法。這對設置靜態變量等是很好的(以線程安全的方式)

你想用 - (id)init來設置它。你正在調用初始化和runTest的類方法中的'self',它們是垃圾或零。讓runTest成爲一個實例方法,我相信你會得到結果。

2 - 使用Charles檢查應用程序進出應用程序。

http://www.charlesproxy.com/

如果你從服務器得到預期的迴應,那麼是的,這是你的應用程序。

+0

代碼(+初始化和其他)直接從HTTPRiot文檔中取得,我更改了代碼以使用類方法而不是實例方法用於委託方法,並且我得到了didReceiveResponse:來調用,但didReturnResource :沒有。 – bwizzy 2009-11-25 02:41:32