2012-02-12 92 views
1

這是我第一次在這個網站上,我對編碼很陌生,所以我想知道是否有人可以幫助我。iOS獲取連接

我想設置從我的iPhone應用程序到我的網站的獲取請求,並獲得從網站回傳給我的手機的信息。

我已經得到了這麼多,但不知道從哪裏去。任何幫助將不勝感激,謝謝!

- (void)myData:(id)sender 
{ 
    NSString *DataToBeSent; 
    sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"]; 
    [receivedData release]; 
    receivedData = [[NSMutableData alloc] init]; 
    DataToBeSent = [[NSString alloc] initWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php?Data=%@",sender]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; 
    [request setHTTPMethod: @"GET"]; 
    NSError *requestError; 
    NSURLResponse *urlResponse = nil; 
    NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError]; 
    [dataToBeSent release]; 
} 

老辦法

- (void)myData:(id)sender 
{ 
    NSString *dataToBeSent; 
    sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"]; 
    [receivedData release]; 
    receivedData= [[NSMutableData alloc] init]; 
    dataToBeSent= [[NSString alloc] initWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php?Data=%@",sender]; 
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent]]; 
    Theconn= [[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; 
    NSLog (@"test1 %@", theRequest); 
    NSLog (@"test2 %@", Theconn); 
    [dataToBeSent release]; 
} 

然後下面的方法被調用,我得到我的數據,但如果我後,我的第一次,但在同一連接上不同的數據發送另一請求時,它總是給我同樣的結果是不應該發生的

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    /* appends the new data to the received data */ 
    [receivedData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)conn 
{ 
    NSString *stringData= [[NSString alloc] 
    initWithData:receivedData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Got data? %@", stringData); 
    [self displayAlertCode:stringData];  
    [stringData release]; 
    // Do unbelievably cool stuff here // 
} 
+0

沒問題。抱歉,我無法解決您的問題。 – 2012-02-12 05:19:32

回答

1

假設正確加載,你可以將數據轉換成字符串,做任何你想做的事情你的數據。

NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError]; 

//Make sure to set the correct encoding 
NSString* responseString = [[NSString alloc] initWithData:response1 encoding:NSASCIIStringEncoding]; 

,如果服務器返回的JSON有可以解析字符串到像NSArray的NSDictionary的和收集的第三方庫。如果你的服務器返回XML,那麼你可以使用NSXMLParser。

編輯

我已經改變了你的代碼有點不同管理內存。

.H

@property (nonatomic,retain) NSMutableData * receivedData; 
@property (nonatomic,retain) NSURLConnection * Theconn; 

.M

@synthesize receivedData; 
@synthesize Theconn; 

//A bunch of cool stuff 


- (void)myData:(id)sender 
{ 
    //If you already have a connection running stop the existing one 
    if(self.Theconn != nil){ 
     [self.Theconn cancel]; 
    } 

    sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"]; 

    //This will release your old receivedData and give you a new one 
    self.receivedData = [[[NSMutableData alloc] init] autorelease]; 


    NSString *dataToBeSent = [NSString stringWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php? Data=%@",sender]; 

    NSURLRequest *theRequest= [NSURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent]]; 

    //This will release your old NSURLConnection and give you a new one 
    self.Theconn = [NSURLConnection connectionWithRequest:theRequest delegate:self]; 

    NSLog (@"test1 %@", theRequest); 
    NSLog (@"test2 %@", Theconn); 

} 

//... 
//Your delegate methods 
//... 

- (void) dealloc{ 
    [receivedData release]; 
    [Theconn release]; 
    [super dealloc]; 
} 
+0

喬爾,當我NSLog response1,它說它爲空。從iphone發送到網站然後由網站生成的數據是一個簡單的字符串。字面上有一個詞,有人命名。謝謝 – jamesHoward 2012-02-12 04:09:11

+0

當你這樣做NSLog時會發生什麼(@「%@」,requestError) – 2012-02-12 04:15:44

+0

kCFRunLoopDefaultMode? – jamesHoward 2012-02-12 04:22:39