2012-03-16 4 views
0

検索バーのユーザー検索テキストをjsonサービスに送信したいと思います。ここで問題はNSDataオブジェクトのnull値を返すことです。問題は何ですか?コンソールで印刷するのと同じURLを定義しても問題はないのですが?NSDataの実際の問題は何ですか

-(void)doIt{ 

    NSURL *url = [NSURL URLWithString:weburls]; 
    NSData *data =[NSData dataWithContentsOfURL:url]; 
    [self getData:data]; 
} 

私はそのように書きます場合、それは動作しますが、私は検索バーのイベントにサービスを呼び出したいけど

NSString *weburl = [NSString stringWithFormat:@"%@%@", 
@"http://192.168.1.196/ravi/iphonephp?mname=",searchText]; 
NSLog(@"%@",weburl); 

NSURL *url = [NSURL URLWithString:weburl]; 
NSLog(@"the url is : %@",url); 

NSError *error; 

NSData *data =[NSData dataWithContentsOfURL:url options:nil error:&error]; 
NSLog(@"Data is :%@",data); 

NSLog(@"the Error massage is : %@",error); 
[self getData:data]; 

は私に

customCellDemo[1553:f803] the url is : http://192.168.1.196/ravi/iphonephp?mname=a 

2012-03-16 15:26:36.259 customCellDemo[1553:f803] Data is :(null) 

2012-03-16 15:26:43.624 customCellDemo[1553:f803] the Error massage is : Error 
Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" 
UserInfo=0x6ab2760 {NSURL=http://192.168.1.196/ravi/iphonephp?mname=a} 
のようなコンソール値を与える問題があります
+0

'+(id)dataWithContentsOfURL:(NSURL *)aURLオプション:(NSDataReadingOptions)マスクエラー:(NSError **)errorPtr' [(documentation)](https://developer.apple.com/library/mac) /documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/doc/uid/20000172-BCIIDGEC)、エラーを確認してください –

答えて

0

からthe manual of dataWithContentsOfURL;

Return Value: A data object containing the data from the location specified by aURL. Returns nil if the data object could not be created.

つまり、NSDataを作成していない可能性があります(おそらく)か、または提供されたURLからデータを取得できない可能性があります。代わりにdataWithContentsOfURL:options:error:を使用してエラーコードを取得し、問題を診断できるようにすることをお勧めします。

+0

このURLは私のブラウザで実行します。問題はここにありますNSDataオブジェクト –

+0

@ sai-info.iphoneあなたのiPhoneブラウザで動作しますか? –

+0

私の電話(シミュレータ)ブラウザのyaも実行され、json Stringを返します。 –

関連する問題