[英]Is testing an app with only an iOS 5 device and iOS 4 simulator OK?
[英]Ok in simulator but throwing exception in ios device
當我在模擬器中運行我的應用程序時,一切運行正常。但是,當我在Ipad中運行同一應用程序時,會拋出異常。
由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'數據參數為nil 。 在我的應用程序中,我必須一步一步請求一個Web-URl,並且需要解析返回的JSON
響應。 但是我已經檢查了網址,並能夠在模擬器中完美解析。 但是所有的問題都已經在真正的ios設備中出現了。但是我想我已經確定了出錯的代碼。
+ (NSDictionary*) getParsedJSON:(NSString*) urlString {
NSLog(@"################################################################################");
NSLog(@"getParsedJSON => urlString:");
NSLog(@"%@", urlString);
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLResponse *response1 = nil;
NSError *error = nil;
NSData* response = [NSURLConnection sendSynchronousRequest:request returningResponse:&response1 error:&error];
//NSData* response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"--------------------------------------------------------------------------------");
NSString* responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"getParsedJSON => responseString:\n%@", responseString);
NSLog(@"--------------------------------------------------------------------------------");
NSError* jsonParsingError = nil;
NSDictionary* parsedJSON = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; // here is place where exception seems to be thrown.
if (jsonParsingError) {
NSLog(@"ERROR in parsing JSON: %@", jsonParsingError);
} else {
NSLog(@"getParsedJSON => parsedJSON: \n%@", [parsedJSON description]);
}
NSLog(@"################################################################################");
return parsedJSON;
}
我已經確定了似乎錯誤的行。我還附上了異常報告的屏幕截圖。 希望得到您有經驗的答復。
首先,您需要在Xcode中設置一個異常斷點-這里有很多關於如何做到這一點的文章。 其次,在每個您在其中創建或返回對象的語句之后,添加一個斷言:
NSURL *foo = ...
assert(foo);
這樣做將幫助您找到第一個問題,而不是最后一個。
正如我們從日志中看到的那樣,當您在設備上使用響應字符串時,響應字符串為null。 這可能是由於某些Internet訪問問題所致。 嘗試使用:
if([response isequaltostring:@"(null)"]||response == nil || response.length == 0)
{
NSError* jsonParsingError = nil;
NSDictionary* parsedJSON = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; // here is place where exception seems to be thrown.
if (jsonParsingError) {
NSLog(@"ERROR in parsing JSON: %@", jsonParsingError);
}
else {
NSLog(@"getParsedJSON => parsedJSON: \n%@", [parsedJSON description]);
}
}
另外,嘗試添加異常斷點並在應用程序崩潰的確切位置進行發布。 讓我知道結果。
根據您的日志,您的響應字符串為空!
做以下兩件事!
如果找到帶有nil值的任何鍵,則'NSJSONSerialization JSONObjectWithData'方法將崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.