我正在使用网络服务获取数据,它从服务器返回一组数据,这在我安装Xcode5之前工作得很好,以前我的应用程序与ios6兼容,现在我更改到iOS7。现在我在从网络服务获取数据时有一个问题。
当我第一次转到UIViewController视图时,我在-(无效)ViewDidLoad中调用web服务,没有数据返回,如果我再调用一次(没有从视图返回,我再次调用相同的web服务),它会返回数据,当我第一次调用它返回空数据,但第二次它返回数据时,这在iOS6中发生了,但同样的代码在iOS7中工作得很好,没有任何web服务问题。
我收到了来自服务器的响应,但里面的数据第一次是空的,但同时我在ios 7设备上运行它的返回数据,所以我不知道我哪里错了?
NSMutableData *serverData;
//delegate methods for NSURLConnection
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *str = [[NSString alloc] initWithData:serverData encoding:NSUTF8StringEncoding];//here i am initializing
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[serverData appendData:data]; // here i get data that is null but i get response from server
}
Myviewwillappear code here...
- (void)viewWillAppear:(BOOL)animated {
[self performSelector:@selector(GetDataFromServer) withObject:nil afterDelay:0.001];
}
- (void) GetDataFromServer {
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
@"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
@"<soap:Body>"
@"<GetProductList xmlns=\"http://tempuri.org/\">"
@"<inCompany>%@</inCompany>"
@"<inUserName>%@</inUserName>"
@"<inType>%@</inType>"
@"<inTypeValue>%@</inTypeValue>"
@"<inSearchVal>%@</inSearchVal>"
@"<inPage>%@</inPage>"
@"</GetProductList>"
@"</soap:Body>"
@"</soap:Envelope>",Company,Username,type,typevalue,searchval,page];
NSURL *url = [NSURL URLWithString:PVBASE_URL];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:PVPRODUCTLIST forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
if (serverData) {
serverData = nil;
}
serverData = [[NSMutableData alloc] init];
}
connection = nil;
} 发布于 2013-09-25 15:55:18
基本上,我已经数次计算了这个问题,因为每次viewDidLoad execute都没有从IBaction获得任何数据,但它得到了显式调用。这与iOS无关。web服务调用的基本问题。
您可能正在使用未执行或初始化的公共变量调用数据。为了更好地理解,请提供您的代码。
谢谢!!
发布于 2013-09-27 10:22:03
我想我会在这里分享我的答案作为一种可能的解决方案。我在使用iOS6时遇到了类似的问题,当我升级到iOS7时,网络堆栈似乎无法工作,因为我没有从NSURLConnection的didRecieveResponse函数获取数据。实际上,通过比较我的代码在iOS平台之间的行为,我只是看起来没有从didRecieveResponse函数接收数据,因为有时expectedContentLength主要返回-1,但在iOS 6中也会发生这种情况。在iOS7中,问题源于与此示例无关的另一个来源,也不是我的网络连接,它影响了我对NSOperation的使用。所以我的观点是,问题可能不是你的平台差异,而是你运行你的功能的代码。我会再检查一遍你的代码,看是否有错误。
https://stackoverflow.com/questions/18999116
复制相似问题