2012-02-12 6 views
1

これは初めてのこのサイトで、誰かが私を助けることができるかどうか疑問に思っていたので、私はこのサイトで初めてです。iOS接続を取得

私のiPhoneのアプリケーションから自分のウェブサイトにget要求を設定し、その情報をウェブサイトから自分の電話に返信したいと思っています。

私はこれまでに得たが、ここからどこに行くのか分からない。どんな助けでも大変感謝しています。ありがとう!

- (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]; 
} 

OLD WAY

- (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]; 
} 

は、その後、次のメソッドが呼び出され、私は自分のデータを取得するが、私は同じ接続上の私の最初の1が、別のデータの後に別の要求を送信した場合、それはいつも私を与えるだろう

- (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

問題ありません。申し訳ありませんが問題を解決できませんでした。 –

答えて

1

データが適切に読み込まれていると仮定すると、データを文字列に変換して、それに必要なものを実行できます。お使いのサーバがJSONを返す場合

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

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

たNSArrayとNSDictionaryのようなコレクションに文字列を解析することができますサードパーティのライブラリがあります。あなたのサーバがXMLを返すならば、NSXMLParserはあなたが使うことのできるものかもしれません。

EDIT

私は少し違ったメモリを管理するようにコードを変更しました。

の.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、それはnullを言います。 iphoneからウェブサイトに送信され、ウェブサイトによって生成されたデータは単純な文字列です。文字通り1つの単語、誰かの名前。ありがとう – jamesHoward

+0

NSLog(@ "%@"、requestError)を実行するとどうなりますか –

+0

kCFRunLoopDefaultMode? – jamesHoward

関連する問題