gpt4 book ai didi

ios - 使用 AFNetworking 解析 XML

转载 作者:技术小花猫 更新时间:2023-10-29 11:09:56 25 4
gpt4 key购买 nike

我是 iOS 的新手,正在尝试执行 REST 请求并获取一些 XML 数据,对其进行解析并理想地放入自定义对象中。但现在我仍然无法获取 XML 数据。

我在 Github/AFNetworking 上找到了这个代码片段..

- (void) fetchInterestData{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFXMLResponseSerializer new];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://www.raywenderlich.com/downloads/weather_sample/weather.php?format=json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}

它可以工作并获取一个 JSON 对象。但我想获取 XML..

如果我使用 http://www.raywenderlich.com/downloads/weather_sample/weather.php?format=xml相反,有一个 xml 对象。但是现在代码完全崩溃了。我希望至少将 xml 作为字符串对象获取。

我需要更改什么才能获取 xml,假设我想获取以下结构的 xml 对象:

<RootNode>
<Banks>
<Bank>
<BankId>17</BankId>
<BankName>Bluestep</BankName>
<BankUrl>http://www.bluestep.se</BankUrl>
<BankImage>
http://smartkalkyl.se/smartfiles/layout/banklogos/bluestep.png
</BankImage>
<Rates>
<Rate>
<RateDate>2013-12-05</RateDate>
<RateType>5</RateType>
<RateInterest>6,23</RateInterest>
<RateDescription/>
<RateBefore>6,27</RateBefore>
<RateChange>False</RateChange>
<RateBeforeDate>2013-08-13</RateBeforeDate>
</Rate>
</Rates>
</Bank>
<Bank>
...

我该怎么做?

更新:新代码..

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
NSString *ua = @"Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25";
[requestSerializer setValue:ua forHTTPHeaderField:@"User-Agent"];
[requestSerializer setValue:@"application/xml" forHTTPHeaderField:@"Content-type"];
manager.requestSerializer = requestSerializer;

NSDictionary *parameters = @{@"foo": @"bar"};


[manager POST:@"http://smartkalkyl.se/rateapp.aspx?user=xxxx&pass=xxx"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSData * data = (NSData *)responseObject;
self.fetchedXML = [NSString stringWithUTF8String:[data bytes]];
//NSLog(@"Response string: %@", self.fetchedXML);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

但这给了我一个错误..

2013-12-06 00:04:10.657 TabbedDemo[38335:a0b] Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/xml" UserInfo=0x8d80ac0 {NSErrorFailingURLKey=http://smartkalkyl.se/rateapp.aspx?user=xxxx&pass=xxxx, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a75230> { URL: http://smartkalkyl.se/rateapp.aspx?user=xxxx&pass=xxxx } { status code: 200, headers {
"Cache-Control" = private;
"Content-Encoding" = deflate;
"Content-Length" = 1260;
"Content-Type" = "text/xml; charset=iso-8859-1";
Date = "Thu, 05 Dec 2013 23:03:47 GMT";
Server = "Microsoft-IIS/7.5";
"Set-Cookie" = "ASP.NET_SessionId=ad3zikxbh4bcawxulkhwt2j3; path=/; HttpOnly";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "UrlRewriter.NET 2.0.0, ASP.NET";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/xml}

知道可能是什么问题吗?

我在应用程序的 Android 版本中执行类似的操作,这是在那里工作的代码。我从不在那里设置任何内容类型..

// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpProtocolParams.setUserAgent(httpClient.getParams(),
System.getProperty("http.agent"));
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);

最佳答案

根据您更新的代码,您需要添加一个响应序列化程序,并且您还需要正确翻译 NSData:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
AFHTTPResponseSerializer * responseSerializer = [AFHTTPResponseSerializer serializer];

NSString *ua = @"Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25";
[requestSerializer setValue:ua forHTTPHeaderField:@"User-Agent"];
// [requestSerializer setValue:@"application/xml" forHTTPHeaderField:@"Content-type"];
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/xml", nil];
manager.responseSerializer = responseSerializer;
manager.requestSerializer = requestSerializer;

NSDictionary *parameters = @{@"foo": @"bar"};

[manager POST:@"http://smartkalkyl.se/rateapp.aspx?user=xxxxx&pass=xxxxxx"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSData * data = (NSData *)responseObject;
self.fetchedXML = [NSString stringWithCString:[data bytes] encoding:NSISOLatin1StringEncoding];
NSLog(@"Response string: %@", self.fetchedXML);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

您的响应以文本/xml 形式返回,需要设置默认响应序列化程序以接受它。

返回的数据不是 UTF8 而是 ascii,因此我们也需要对其进行适当的设置。

我在模拟器中尝试了一下,它在我这边运行良好。

编辑:数据似乎是 ISO latin-1 格式。我的错。

关于ios - 使用 AFNetworking 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20402034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com