0
私は自分のWebサイトから私が解析する必要があるデータを取得する私のiPhoneに接続を作成したいと思います。私は、これまでの任意の運を持って、次のデリゲートメソッドに関して一種の混乱していないのです。URL接続ios
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
これらのメソッドは、私のコードそこから自分に求めているか、私はそれらを手動で呼び出す必要がありますか? .hファイルのどこにでもデリゲートを宣言する必要がありますか? これは私がやっていることですが運がなかったことです。誰かがそれを理解できればそれは高く評価されるだろう。私の接続は成功したが、NSLogはdidFailWithErrorのコンソールに表示されます。
おかげ
-(void) data: (id) sender
{
NSString *stringToBeSent;
NSURL *siteWithNumbers;
NSString *translation;
NSError *error;
NSString *boo;
sender= [sender lowercaseString];
sender= [sender stringByReplacingOccurrencesOfString:@"," withString:@""];
receivedData= [[NSMutableData alloc] init]; //declared in .h file as NSMutableData
stringToBeSent= [[NSString alloc]
initWithFormat:@"http://xxxx/sql.php? data=%@",sender];
NSURLRequest *theRequest=[NSURLRequest
requestWithURL:[NSURL URLWithString:stringToBeSent]];
NSURLConnection *conn= [[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
//[self createConnectionWithPath:stringToBeSent];
if(conn)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection could not be made");
}
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
/* appends the new data to the received data */
NSLog(@"here now1");
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
NSString *stringData= [[NSString alloc]
initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"Got data? %@", stringData);
[conn release];
conn = nil;
}
- (void)connection:(NSURLConnection *)
connection didFailWithError:(NSError *)error
{
NSLog(@"fail");
}
デリゲートメソッドは、NSURLConnectionによって呼び出されます。手動で呼び出すことはありません。 'if(conn)'は、NSURLConnectionオブジェクトが正常に作成されたことを意味し、接続が「作成され、データが正常に送受信された」ことを意味しません。 didFailWithErrorのエラーを記録すると、問題の内容を知ることができます: 'NSLog(@" fail、error =%@ "、error);' – Anna