2012-03-10 18 views
1

私は、リクエストに応じてYESまたはNOを返すPHP Webサイトを持っています。 例えばObjective-CでPHPのウェブサイトの反応を入手

www.test.com/test.php?variable=test1 はYES私を返します とwww.test.com/test.php?variable=test2 は私にNO

を返しませんIこのレスポンスを私が作成しているObjective-Cアプリケーションに入れようとしていますが、今まで運がありません。 ここに私のコードです

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.test.com/test.php?variable=test1"] 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    NSMutableData *receivedData = [NSMutableData data]; 
    NSString *strData = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding]; 
    NSLog(@"This is the response: %@", strData); 
}else { 
} 

誰でも助けてくれますか?それを行う他の方法はありますか?私は何か間違っているのですか?

おかげでたくさんの男

+0

「今まで運がない」と言ったら、どうしましたか?クラッシュ、 "これはレスポンスです:"決して印刷されませんか?デリゲートがデータを受け取るように設定されていますか? – gaige

答えて

2
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
     { 
      [receivedData appendData:data]; 
     // declre receivedData as a property of the class(in xxx.m) 
      NSError *error=nil; 
      NSDictionary *result=[NSJSONSerialization JSONObjectWithData:data options: 
            NSJSONReadingMutableContainers error:&error]; 
      NSLog("%@", result); 
    //do whatever you want... 

      //the result dictionary is a JSON object and you can use it as KVC(key-value-coding) rules 
} 

あなたは、要求を作成します。また、iOSの他のバージョンで使用したい場合は、JSONパーサフレームワークをご覧ください。

+0

それを詳しく教えていただけますか?私はそれを使用しようとしていますが、多くのエラーが発生します。 – user1015777

+0

あなたの... .hファイルはreceivedDataとsysnthesyzeのプロパティを.mで宣言しています..そしてreceivedDataはnsmutabledataで、nsmutabledictionary辞書)。 –

0

あなたが正しくあなたの接続を設定しているようです。しかし、あなたがしてNSURLConnection.hファイルに記載NSURLConnectionデリゲートメソッド内のデータをキャッチする必要があります。

特に興味深いの
@protocol NSURLConnectionDataDelegate <NSURLConnectionDelegate> 
@optional 
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response; 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 

- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request; 
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten 
               totalBytesWritten:(NSInteger)totalBytesWritten 
             totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite; 

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse; 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection; 
@end 

didReceiveDataconnectionDidFinishLoadingです。アプリのターゲットのiOS 5がちょうど法の下であなたのxxx.mファイルにこのコードを使用する場合

関連する問題