如何将XML POST数据从iOS应用程序发送到Django应用程序?

时间:2011-05-08 07:14:43

标签: iphone objective-c django ios django-views

我正在尝试在iOS游戏应用中实现在线排行榜,使用Django处理来自iDevice的POST请求并存储分数。我已经想出如何让Django将对象序列化为XML,我的iPhone可以读取和显示分数。但是,我不能为我的生活让我的iPhone将POST XML发送到我的Django服务器。

以下是我用来发布分数的功能......

iOS(Objective-C)控制器:

- (void) submitHighScore {
    NSLog(@"Submitting high score...");

    NSString *urlString = HIGH_SCORES_URL;
    NSURL *url = [NSURL URLWithString: urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
    [request setHTTPMethod: @"POST"];
    [request setValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];

    NSMutableData *highScoreData = [NSMutableData data];
    [highScoreData appendData: [[NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"] dataUsingEncoding: NSUTF8StringEncoding]];
    [highScoreData appendData: [[NSString stringWithFormat: @"<player_name>%@</player_name", @"test"] dataUsingEncoding: NSUTF8StringEncoding]];
    [highScoreData appendData: [[NSString stringWithFormat: @"<score>%d</score>", 0] dataUsingEncoding: NSUTF8StringEncoding]];
    [highScoreData appendData: [[NSString stringWithFormat: @"</xml>"] dataUsingEncoding: NSUTF8StringEncoding]];
    [request setHTTPBody: highScoreData];

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: YES];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest: request 
                                                                  delegate: self];
    if (!connection) {
        NSLog(@"Request to send high scores appears to be invalid.");
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO];
    }
}

上述方法成功发送请求,并将其正确解释为CONTENT_TYPE: text/xml,但处理请求的Django视图似乎无法理解它,解释它几乎就像它只是纯文本。下面是我的Django视图......

Django(Python)视图:

from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core import serializers
from django.core.exceptions import ValidationError
from django.views.decorators.csrf import csrf_exempt

from modologger.taptap.models import HighScore

@csrf_exempt
def leaderboard( request, xml = False, template_name = 'apps/taptap/leaderboard.html' ):
    """Returns leaderboard."""
    if xml == True: # xml is set as True or False in the URLConf, based on the URL requested
        if request.method == 'POST':
            postdata = request.POST.copy()
            print postdata
                # here, postdata is evaluated as: 
                # <QueryDict: {u'<?xml version': [u'"1.0" encoding="UTF-8" ?><player_name>test</player_name<score>0</score></xml>']}>
            for deserialized_object in serializers.deserialize('xml', postdata):    # this fails, returning a 500 error
                try:
                    deserialized_object.object.full_clean()
                except ValidationError, e:
                    return HttpResponseBadRequest
                deserialized_object.save()
        else:   
            high_score_data = serializers.serialize( 'xml', HighScore.objects.all() )
            return HttpResponse( high_score_data, mimetype = 'text/xml' )
    else:
        high_scores = HighScore.objects.all()
        return render_to_response( template_name, locals(), context_instance = RequestContext( request ) )

老实说,我不确定问题是在Objective-C还是在Django代码中。 Objective-C是不是以正确的格式发送XML?或者Django服务器没有正确处理该XML?

非常感谢任何见解。提前谢谢。

更新

我让它工作,通过编辑iOS控制器来设置请求的HTTPBody,如下所示:

NSMutableData *highScoreData = [NSMutableData data];
[highScoreData appendData: [[NSString stringWithFormat: @"player_name=%@;", @"test"] dataUsingEncoding: NSUTF8StringEncoding]];
[highScoreData appendData: [[NSString stringWithFormat: @"score=%d", 0] dataUsingEncoding: NSUTF8StringEncoding]];
[request setHTTPBody: highScoreData];

出于某种原因,在其中添加分号让Django识别它,将值分配给HighScore类的新实例,并保存它。测试服务器上的日志记录表明request.POST<QueryDict: {u'score': [u'9'], u'player_name': [u'test']}>

还不太清楚这一切是怎么做的。

根据Radu's的建议,我在使用NSLog查看了highScoreData,然后将其附加到request.HTTPBody,结果为<706c6179 65725f6e 616d653d 74657374 3b73636f 72653d39>

我是一个巨大的Obj-C菜鸟,所以再次感谢任何帮助!再次感谢。

1 个答案:

答案 0 :(得分:1)

既然你控制了双方,我就会降低xml编码数据的复杂性,并使用RestKit或其他一些框架来简化与Django的沟通。