在Objective-C中,XML解析没有按预期工作

时间:2011-02-23 06:23:21

标签: iphone objective-c xml xml-parsing

我是iPhone编程新手,这是第一次解析XML文件。我已经添加了url并将XML文件硬编码到字符串中,但是我没有从服务器获得正确的响应。

这是我的代码:

NSString *post = @"<?xml version=\"1.0\"encoding=\"UTF-8\"?<request><call>GetNewChapters</call><udid>1000000000000000000000000000000000000000</udid><book_id>1</book_id><updatetoken>B20100125054802</updatetoken></request>";

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSURL *url=[NSURL URLWithString:@"https://www.paisible.com/babelle_api"];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];    
[request setHTTPBody:postData];



NSString *myStr = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];
NSLog(@"String Value :%@",myStr);


NSLog(@"theRequest: %@", request);
NSURL *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(theConnection)
{
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"theConnection is NULL");
}

webdata是NSMutable数据的位置。请让我知道我在解析代码中犯了哪些错误。

1 个答案:

答案 0 :(得分:1)

尝试替换

NSURL *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

以下内容:

NSURLResponse *resp;
NSError *error;
NSMutableData *webData = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&error]; 

然后,检查“webData”包含的内容。