【发布时间】:2013-09-25 07:46:27
【问题描述】:
我正在使用 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;
}
【问题讨论】:
-
能发一些相关的sn-ps代码吗?
-
@CarlJ 我添加了有问题的代码,我收到了来自服务器的响应,但在 xml 标记中,数据为空,这是下一次第一次获取数据时,所以每当我第一次去那个视图时,返回数据包含空ios 6 设备,我在 ios 7 设备上进行了测试,我也没有遇到这样的问题。那你能告诉我哪里错了吗???
-
我遇到了一个相关问题,即我的 didReceiveResponse 函数从服务器获得响应,但没有附加数据。在标题中没有内容长度?此功能在 iOS 6 中运行良好,但在 iOS 7 和 Xcode 5 中,我遇到了这个问题。我想听更多?
标签: iphone web-services ios6 ios7 xcode5