简体   繁体   English

使用AFNetworking解析Content-Type text / html

[英]Parsing Content-Type text/html with AFNetworking

I have to implement a version checker in my app. 我必须在我的应用程序中实现版本检查器。 The app will check the version (which was manually set) from the server. 该应用程序将从服务器检查版本(已手动设置)。 Thing is, the server return the response in neither JSON or XML . 事实是,服务器不以JSONXML返回响应。 It returns in plain text/html , which I'm not familiar with. 它以纯text/html返回,我不熟悉。

eg. 例如。 If the app need to be updated, it will return response as below. 如果需要更新应用程序,它将返回如下响应。

<checkversion>
    <update>TRUE</update>
    <status>MINOR</status>
    <alert></alert>
    <version>1.0.0</version>
    <ituneslink>0</ituneslink>
</checkversion>

Else, it will return response as below. 否则,它将返回如下响应。

<checkversion>
    <update>FALSE</update>
</checkversion>

After looking at some similar SO posts such as this and this , I manage to get the response in format of string . 看了类似thisthis的类似SO帖子后,我设法以string格式获取响应。 Below is the snippets of my code. 以下是我的代码片段。

- (void)slurpVersionChecker
{
    NSString *versionURL = @"api";
    NSDictionary *params = @{@"api_key":@"key"};

    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    [requestManager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    [requestManager POST:versionURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"version: %@", string);

    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSLog(@"failed to check version: %@", error);
    }];

}

Question is, how do I parse each of the responses? 问题是,如何解析每个响应? and is it there any way where I can parse the response through responseObject directly without converting it to String ? 是否有任何方法可以直接通过responseObject解析responseObject而无需将其转换为String

Okay. 好的。 I already managed to solve this so I might as well post the solution here for others. 我已经设法解决了这个问题,所以我不妨在这里为其他人发布解决方案。

Here's what I basically do. 这基本上是我要做的。

I use XMLReader to parse the response into NSDictionary. 我使用XMLReader将响应解析为NSDictionary。 So, add the library to my project and import XMLReader.h in the header. 因此,将库添加到我的项目中,并在标题中导入XMLReader.h Then, I only need to call the dictionaryForXMLDAta method to use it which returns: 然后,我只需要调用dictionaryForXMLDAta方法即可使用它返回:

checkversion = {
version: {
    alert =     {
        text = "";
    };
    ituneslink =     {
        text = 0;
    };
    status =     {
        text = MINOR;
    };
    text = "";
    update =     {
        text = TRUE;
    };
    version =     {
        text = "1.0.0";
    };
}

The NSDictionary return is not that pretty though. NSDictionary的回报不是那么漂亮。 So, I had to use objectForKey a few times to retrieve the value. 因此,我不得不多次使用objectForKey来检索值。

[requestManager POST:versionURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData * data = (NSData *)responseObject;
        NSError *error = nil;
        NSDictionary *dict = [XMLReader dictionaryForXMLData:data
                                                     options:XMLReaderOptionsProcessNamespaces
                                                       error:&error];
        NSDictionary *version = [dict objectForKey:@"checkversion"];
        update = [[version objectForKey:@"update"] objectForKey:@"text"];
        ...
    }

Bonus: I want to notify the user if there's a new update available. 奖励:我想通知用户是否有新的更新。 So, I check if the update variable is equal to TRUE and if it's TRUE , it will link the user to the app store. 因此,我检查update变量是否等于TRUE ,如果它为TRUE ,它将把用户链接到应用商店。

if([update isEqualToString:@"TRUE"]) {
            number = [[version objectForKey:@"version"] objectForKey:@"text"];
            itunesURL = [[version objectForKey:@"ituneslink"] objectForKey:@"text"];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New Version Available" message:nil delegate:self cancelButtonTitle:@"Not Now" otherButtonTitles:@"Download", nil];
            [alert show];

        }

And that's pretty much how I solved it. 这就是我解决问题的方式。

Please try the following code: 请尝试以下代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager new];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM