2011-02-11 8 views
0
NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1]; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"text/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"text/json" forHTTPHeaderField:@"Accept"]; 

    NSError  *error ; 
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error]; 
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
    NSLog(@"jsondata=%@,%@",json_string,response); 

2011-02-11 12皆無:01:16.653 mystack [1946:207] jsondata =、(ヌル)あなたは何を参照してくださいエラー変数をのNSLogすべきJSONデータが返信

+0

あなたのサーバの応答に何か問題があるようです。 NSDataインスタンスが空であるかどうかを確認するために、** [レスポンスの長さ] **のようにレスポンスのファイル長をNSLogに設定することをお勧めします。 –

+2

[レスポンス長]は0 – GameLoading

+1

ご質問ください。 – JeremyP

答えて

3

おかげで答え

[request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

私がする必要があるコードです

1

起こった:ここで私を助けるため[error localizedDescription]

+0

jsondata =、(null)、0、エラードメイン= NSURLErrorDomainコード= -1000 "不正なURL" UserInfo = 0x4b65150 {NSUnderlyingError = 0x4b68fd0 "不正なURL"、NSLocalizedDescription =不正なURL} – GameLoading

4

URL文字列には空白が含まれているため、無効です。&NSURLオブジェクトを作成する前にそれらをエスケープする必要があります。置き換え:

NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1]; 

で:

NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1]; 
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];