从url xcode获取数据

时间:2011-05-09 13:05:29

标签: objective-c ipad download

我正在尝试从与php一起使用的在线链接加载数据。该链接返回一个xml。 那个xml我想从iPad保存在Documents Directory中。 我正在尝试这个:

    NSString *post = @"";
    NSData *postData = [post dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:NO];
    NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
    [urlRequest setURL:[NSURL URLWithString:@"http://www.test.be/tools/xml_nieuwsbrief.php"]];
    [urlRequest setHTTPMethod:@"POST"]; 
    [urlRequest setHTTPBody:postData];


    NSData *urlData; 
    NSURLResponse *response; 
    NSError *error;
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; 
    if(!urlData) {
        NSLog(@"No connection!");
    }



    NSLog(@"Receive:%d bytes",[urlData length]);
    NSString *aStr = [[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]autorelease]; 
    NSLog(@"________%@", aStr);

字符串确实返回 - null - ,我一直在搜索互联网,但找不到任何可以引导我解决问题的东西。

有人可以帮我看看我做错了吗?

提前致谢,

多雪

1 个答案:

答案 0 :(得分:0)

我会检查响应对象的状态代码(将NSURLResponse对象更改为NSHTTPURLResponse,并且您将拥有statusCode属性)。

如果您实际上从NSData响应中获取 - null - 您可能应该查看服务器端脚本。也许它不期望一个空的POST请求?

相关问题