我正在使用 Web 服务来获取数据并从服务器返回一组数据,这在我安装 xcode 5 之前效果很好,以前我的应用程序与 ios6 兼容,现在我更改为 ios 7。现在我在从中获取数据时遇到了一个问题网络服务 。
当我第一次进入 UIViewController 视图时,我在 -(void)ViewDidLoad 中调用 Web 服务并且没有数据返回,如果我再调用一次(没有从视图返回,我再次调用相同的 Web 服务)它返回数据,当我第一次调用它返回空数据,但第二次返回数据,这发生在 ios 6 中,但相同的代码在 ios 7 中没有任何 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;
}