我想从一些 url 中获取一些数据并在 nslog 中打印结果。我正在传递 URl,并且只想在日志中获取结果。我用过这段代码:
-(void)GETJSONDATA
{
NSString*lu=@"tmp";
NSString *requestString = [[NSString alloc]init];
// [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"WRONGANSWER"];
NSLog(@"request string:%@",requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString: @"http://assessments.tlisc.org.au/webservices/questions/getbytaskpart.php?jsoncallback=?&token=1726204214321678|xTAieBBJoDaWmBsG1stxfq4zLO4&taskpartid=1"]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];
NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
if (respError)
{
// NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@",
// [respError localizedDescription],
// [[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Test" message:@"check your network connection" delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
else
{
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"Resp : %@",responseString);
NSDictionary *results = [responseString JSONValue];
NSLog(@"results=%@",results);
}
}
它向我显示了这个错误:
-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x6a42be0 {NSLocalizedDescription=Unexpected end of string}"
有人可以指出我的错误吗?在响应字符串中,它显示空值。我不知道实际的错误。
它在浏览器中工作正常但是当我解析它时它显示这个错误......无论如何我可以通过它修改url并获得结果......我已经用不同的url检查了我的代码。它工作正常..