Web サーバーから JSON データを取得する iPhone アプリを開発しており、NSURLConnectionDelegate、NSURLConnectionDataDelegate、NSURLConnectionDownloadDelegate プロトコルを使用しています。ローカルホストを使用してデータを正常に取得できますが、インターネット Web サーバーからは取得できません。
NSURL*url=[NSURL URLWithString:urlString];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSLog(@"%@",theRequest);
// Create the connection with the request and start loading the data.
// NSURLDownload *theDownload = [[NSURLDownload alloc] initWithRequest:Request delegate:self];
NSURLConnection* connection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
その他のデリゲート メソッドは以下のとおりです。
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
mydata =[[NSMutableData alloc] init];
[mydata setLength:0];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView*errorView =[[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not be completed- please make sure you are connected to Internet either 3G or Wi-fi" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection{
NSArray*jsonArr =[[NSArray alloc] init];
NSError*error;
jsonArr= [NSJSONSerialization JSONObjectWithData:mydata options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",error);
NSLog(@"Json Array %@",jsonArr);
self.questions = [NSMutableArray arrayWithArray:jsonArr];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
次のエラーが表示されます: エラー ドメイン=NSCocoaErrorDomain Code=3840 "操作を完了できませんでした。(Cocoa エラー 3840.)" (文字 3 付近の値が無効です。) UserInfo=0x71be760 {NSDebugDescription=文字 3 付近の値が無効です。}
これは、インターネット Web サーバーからデータを試行するときにのみ発生し、localhost へのアクセスでは発生しません
All the code works perfectly if I use url as http://localhost/example.php but as soon as
I change the url to use the web server url http://www.example.net/data.php
although there is no problem with connection but it returns data as nil.
どんな提案でも大歓迎です!