這是我第一次在這個網站上,我對編碼很陌生,所以我想知道是否有人可以幫助我。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 //
}
沒問題。抱歉,我無法解決您的問題。 – 2012-02-12 05:19:32