0
我想创建连接从我的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