2012-07-01 28 views
1

我是ObjC的新手,並且正在編寫一個簡單的CONSOLE應用程序,以便從網絡獲取數據並解析數據,或者使用與之相關的操作。我正在嘗試使用NSURLConnection,但在獲取任何數據時遇到問題。我使用TCPDUMP來捕獲流量,並且我發現請求甚至沒有發送出去,因此我甚至沒有在控制檯中得到任何結果。我不是想在Mac上創建一個簡單的控制檯應用程序ios應用程序。任何幫助將不勝感激。 **我在這個項目上使用Xcode v4.2和ARC。Objective C和NSURLConnection

的main.m:

#import <Foundation/Foundation.h> 
#import "HTTPRequest.h" 

int main(int argc, const char * argv[]) 
{ 
    @autoreleasepool { 
    HTTPRequest *http = [[HTTPRequest alloc]init]; 
    [http doMagic ]; 
    } 
    return 0; 
} 

HTTPRequest.h:

#import <Foundation/Foundation.h> 
    @interface HTTPRequest :NSObject <NSURLConnectionDelegate> { 
     NSMutableData *webData; 
     NSURLConnection *conn; 
    } 

    -(void) doMagic; 

    @end 

HTTPRequest.m:

#import "HTTPRequest.h" 
@implementation HTTPRequest 
-(void) doMagic { 
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; 
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
    conn = [[NSURLConnection alloc] initWithRequest:req 
              delegate:self]; 
    if (conn) { 
     webData = [NSMutableData data]; 
     NSLog(@"DEBUG: %@", [webData length]); 
     } 

    } 

    -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
     [webData setLength:0]; 
    } 

    -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
     [webData appendData:data]; 
    } 
    -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
     NSLog(@"Connection failed! Error - %@ %@", 
     [error localizedDescription], 
     [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
    } 

    -(void) connectionDidFinishLoading:(NSURLConnection *) connection { 

    NSLog(@"Succeeded! Received %lu bytes of data",[webData length]); 

    NSLog(@"DONE. Received Bytes: %lu", [webData length]); 
    NSString *theData = [[NSString alloc] initWithBytes:[webData mutableBytes] 
               length:[webData length] 
               encoding:NSUTF8StringEncoding]; 
    // -prints html received -- 
    NSLog(@"%@", theData); 
    } 
    @end 
+0

您可能需要調用'[conn fire];' –

+2

您需要運行循環才能使NSURLConnection工作。 GUI應用程序默認設置了一個運行循環,但命令行工具並非如此。看看'NSRunLoop'。 – omz

+0

Thx omz。我認爲你正在做的事情,因爲我創造了與iOS完全相同的東西,它工作得很好,但由於某種原因,它不能在控制檯應用程序工作,我認爲你是正確的NSRunLoop(從我最初的谷歌搜索)。我會看看NSRunLoop並回報。 Thx男人。非常感謝您的幫助。 –

回答

0

我通常使用日e startImmediately參數爲我的連接(如在,使用initWithRequest:delegate:startImmedately:NSURLConnection。)也許嘗試與此?如果不是,您可能需要明確地致電start

此外,我不確定這是否與您的問題有關,但您不保留webData。 (你正在使用一個自動釋放指針初始化它,它可以在任何NSURLConnectionDelegate回調之前被釋放。)

+0

感謝圖洛克斯。我確實試過startImmediately和明確的調用開始,但不起作用:(另外我無法保留webData,因爲我使用的是ARC。 –

1

感謝OMZ。 NSRunLoop是答案。發現它偉大的文章在這裏: http://coc24hours.blogspot.com/2012/01/using-nsrunloop-and-nsurlconnection.html

而對於修復:

剛剛將該僅供測試,它的if語句內的工作完美的罰款:

if (conn) { 
     webData = [NSMutableData data]; 
     NSRunLoop *loop = [NSRunLoop currentRunLoop]; 
     [loop run]; 
     NSLog(@"DEBUG: %@", [webData length]); 
    } 

感謝大家的幫幫我。