【发布时间】:2009-07-09 19:05:00
【问题描述】:
我想问你关于 iPhone 的objective-c 中的NSURLConnection。
我有一款应用需要连接到一项网络服务以接收数据(关于 YouTube 视频), 然后我就有了我需要连接的所有东西(类似于网络中的 Hello_Soap 示例代码)。
但是现在,我的问题是我创建了一个名为 Connection 的类(继承自 NSObject)并且我已经实现了这些方法:
didReceiveResponse、didReceiveData、didFailWithError 和 connectionDidFinishLoading。还有方法:
-(void)Connect:(NSString *) soapMessage{
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://....."];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
但是当我从我的 AppDelegate 创建一个 Connection 对象时:
Connection * connect = [[Connection alloc] Init:num]; //It's only one param to test.
[connect Connect:method.soapMessage];
我调用这个方法,当它完成时,它不会继续调用didReceiveResponse、didReceiveData、didFailWithError 或connectionDidFinishLoading。
我正在尝试这样做,但我暂时做不到。
我想做的事情是能够在每次我想接收数据时调用这个类“连接”(之后被解析并显示在UITableViews 中)。
谢谢。
【问题讨论】:
-
请同时发布您的委托方法。此外,Objective-C 约定是方法名称以小写开头。
标签: ios objective-c nsurlconnection nsurlconnectiondelegate