2014-12-03 62 views
0

我发送一个NSURLSessionDataTask,并且我得到NSData作为响应。它应该包含XML。但我无法得到它。从NSURLSessionDataTask请求的NSData响应获取XML

当我发送命令行上一个curl请求时,它按预期工作

NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //5 
    if (!error) { 
     NSLog(@"%@", [response description]); 
     NSLog(@"%@", data); // some data I can not understand. 
    } 
}]; 

请求:

{ 
    "Content-Type" = "application/xml; charset=\"utf-8\""; 
} 

响应

{ status code: 207, headers { 
    "Accept-Ranges" = bytes; 
    Connection = close; 
    "Content-Length" = 1282; 
    "Content-Type" = "text/xml"; 

请提供指针

回答

3

text/xml仅仅是XML字符串,因此通过

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

响应数据转换为字符串您的代码将是:

NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //5 
    if (!error) { 
     NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@", responseString); 
    } 
}]; 
+0

感谢...我注意到,刚才......将接受答案 – GJain 2014-12-03 10:11:40