gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:12:40 25 4
gpt4 key购买 nike

我正在尝试在 iOS 游戏应用程序中实现在线排行榜,使用 Django 处理来自 iDevice 的 POST 请求并存储分数。我已经弄清楚如何让 Django 将对象序列化为 XML,并且我的 iPhone 可以读取和显示分数。但是,我这辈子都无法让我的 iPhone 将 XML 发布到我的 Django 服务器。

下面是我用来发布分数的函数...

iOS (Objective-C) Controller :

- (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 View 似乎无法理解它,几乎将其解释为纯文本。下面是我的 Django View ...

Django(Python) View :

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 Controller 来设置请求的 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 新手,所以再次感谢任何帮助!再次感谢。

最佳答案

由于您控制双方,我会放弃 xml 编码数据的复杂性,并使用 RestKit 或其他一些可以轻松与 Django 通信的框架。

关于iphone - 如何将 XML POST 数据从 iOS 应用程序发送到 Django 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5926140/

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