下面的代码我用来向服务器发布数据,它工作正常。但有时应用程序崩溃并且我得到此错误"'NSInvalidArgumentException', reason: 'data parameter is nil'".
我检查了互联网连接,但互联网连接是好的毫无疑问的连接。如何解决这个问题。
NSString *url = [NSString stringWithFormat:@"http://myurl.com/test"];
NSString *username = [_emailLoginString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *password = [_passwordLoginString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableString* requestURL = [[NSMutableString alloc] initWithString:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithString:requestURL]]];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:@"username=%@&password=%@",
username,password] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&err];
NSString *serverRplyLoginString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
NSDictionary *dictobj=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];
_serverRplyLoginString=[dictobj objectForKey:@"error"];
NSLog(@"Login Response Is :%@",_serverRplyLoginString);
答案 0 :(得分:1)
首先,我建议您始终启用异常断点:
使用异常断点可以更容易地修复崩溃,因为应用程序将暂停在导致异常的确切行。现在,让我们回到崩溃中:
您的应用崩溃了,因为与消息一样,数据参数为零。哪里?这里:
$(function () {
$("#someStaticParentContainer").on("click", "li.k-state-selected",function () {
$("#someDivId").append($(this).find("img").clone());
});
});
虽然您可以创建数据参数为nil的NSDictionary *dictobj=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];
:
NSString
您无法使用带有nil数据参数的json序列化创建NSString *serverRplyLoginString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
。我建议您检查响应是否为零并正确处理这种情况。
哦,还有最后一件事。我强烈建议您使用同步请求超过同步,因为后者将冻结主队列(即使您的应用程序口吃)。您可以像这样制作一个简单的异步请求:
NSDictionary
答案 1 :(得分:0)
发出请求的代码:
NSString *url = [NSString stringWithFormat:@"http://myurl.com/test"];
NSString *username = [_emailLoginString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *password = [_passwordLoginString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableString* requestURL = [[NSMutableString alloc] initWithString:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithString:requestURL]]];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:@"username=%@&password=%@",
username,password] dataUsingEncoding:NSUTF8StringEncoding]];
修改以更好地处理错误
// declared so that chunk data will be handled
__block NSMutableData *fragmentData = [NSMutableData data];
[[NSOperationQueue mainQueue] cancelAllOperations];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
//in case your data is chunked
[fragmentData appendData:data];
if ([data length] == 0 && error == nil)
{
NSLog(@"No response from server");
}
else if (error != nil && error.code == NSURLErrorTimedOut)
{
NSLog(@"Request time out");
}
else if (error != nil)
{
NSLog(@"Unexpected error occur: %@", error.localizedDescription);
}
// response of the server without error will be handled here
else if ([data length] > 0 && error == nil)
{
// if all the data was successfully gather without error
if ([fragmentData length] == [response expectedContentLength])
{
// finished loading all your data
// handle your response data here, in this example it's `fragmentData`
NSString *serverRplyLoginString = [[NSString alloc] initWithData:fragmentData/*responseData*/ encoding:NSASCIIStringEncoding];
NSDictionary *dictobj=[NSJSONSerialization JSONObjectWithData:fragmentData/*responseData*/ options:kNilOptions error:&err];
NSLog(@"Login Response Is :%@",serverRplyLoginString);
NSLog(@"dictobj :%@",dictobj);
}
// if fragmentDatas length is not equal to server response's expectedContentLength
// that means it is a chunked data and the other half of the data will be reloaded and `[fragmentData appendData:data];` will handle that
}
}];