我想知道如何做下一个:
我有一个应用程序可以建立一些连接来接收数据(此数据显示在屏幕上)
我想知道我可以用来等待每个连接的代码,我的意思是,连接开始和完成之间的时间我想在屏幕上显示一次旋转(正在加载......)。
我创建了连接和旋转。我的问题是,我不知道可以使用哪些代码来管理这个以及在哪里编写这段代码。
谢谢!
答案 0 :(得分:0)
您应该看一下NSURLConnection与委托一起使用。 NSURLConnection允许您异步从服务器获取数据(它在后台运行,并在发生特定事件时通知代理)。
然后,在视图控制器类中,您可以在启动连接之前启动微调器,并让其中一个委托方法在连接完成时停止微调器。
答案 1 :(得分:0)
这是我在类中的代码:Connection.h,我在每个类中创建一个对象Connection,我想调用一个新连接来获取数据(我不知道这是否是正确的方法) )
导入“Connection.h” 导入“XMLParser.h”
@implementation连接 @synthesize webData,soapResults,xmlParser;
- (Connection *)Init:(NSInteger *)methodNumber { [super init]; methodNum = methodNumber; 回归自我; }
- (void)Connect:(NSString *)soapMessage {
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://.....?WSDL"];
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 connectionWithRequest:theRequest delegate:self];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[theConnection start];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; }
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@“ERROR with the Conction”); [连接发布]; [webData发布]; }
- (void)connectionDidFinishLoading:(NSURLConnection *)连接 {
NSLog(@“DONE。收到字节:%d”,[webData长度]);
NSString * theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
if( xmlParser )
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser:methodNum];
//Set delegate
[xmlParser setDelegate:parser];
//[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
[connection release];
[webData release];
}
@end