【发布时间】:2012-10-11 11:28:03
【问题描述】:
这个问题与另一个问题有关:Zend getParameters not returning what is in url route
我面临的问题是,Zend 服务器随机没有获取路由中的参数,导致我的应用程序登录崩溃。即,有时在打印 getParams(); 函数时,我会收到如下用户电子邮件:
Array (
[email] = user@email.com
(...)
)
这很好用,但其他时候我得到了这个:
Array (
[user@email.com] = 2.874983 //Longitude
(...)
)
导致登录操作崩溃。
这可能是服务器端或客户端的故障。每次调用服务器时,我都会放置一个NSLog 来打印NSMutableURLRequest,并且我已经看到请求网址总是格式正确的。也许这是一个巧合,我不知道,但有时试图解决这个问题我已经更改了请求代码并暂时修复了它。但是,在一些请求之后,错误又回来了。
下面的 sn-p 代表我用来连接服务器的代码:
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self._url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSMutableString *values = nil;
if ( params ) {
NSDictionary *parameters = params;
values = [[NSMutableString alloc] init];
for ( NSString* key in parameters ) {
NSMutableString *value = [[NSMutableString alloc] init];
[value appendString:key];
[value appendString:@"="];
[value appendString:[self urlEncodeValue:[parameters valueForKey:key]]];
[value appendString:@"&"];
[values appendString:value];
}
}
NSData *postData = [values dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLenght = [NSString stringWithFormat:@"%d", [postData length]];
if ( cookies ) {
NSDictionary *cooks = cookies;
for ( NSString* cookie in cooks) {
NSMutableString *ck = [[NSMutableString alloc] initWithString:cookie];
[ck appendString:@"="];
[ck appendString:[cooks valueForKey:cookie]];
[theRequest addValue:ck forHTTPHeaderField:@"Cookie"];
}
}
[theRequest setHTTPMethod:self._method];
if ( values ) {
[theRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody: postData];
[theRequest setValue:postLenght forHTTPHeaderField:@"Content-Length"];
}
// create the connection with the request
// and start loading the data
NSLog(@"%@", theRequest);
NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
另一个重要的事情是登录会发送纬度和经度值,当其中一些值发生变化时,它似乎会崩溃。
【问题讨论】:
-
您看到了什么异常?崩溃是什么样的(原因、堆栈跟踪)?
-
当 Zend 服务器验证参数时,应用程序崩溃,因为它找不到
[email]参数。当参数以这种方式到达[user@email.com] = 2.874983而不是[email] = user@email.com时,就会发生这种情况 -
我在您的问题中看到了这一点。我无法帮助处理服务器返回值,但如果您描述了实际的 iOS 错误,我或许可以提供一些关于防止崩溃的建议。
-
好吧,iOS 本身没有错误,我在我的应用程序中正确接收到 Zend_Exception 的结果并向用户显示登录错误消息。我认为问题可能来自 iOS 代码中的错误 NSURLRequest...
-
啊……对不起。我看到“崩溃”这个词,还以为你的意思是“崩溃”。 :-)
标签: ios zend-framework ios5 nsurlconnection