2012-02-12 59 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