【发布时间】:2014-10-17 10:03:48
【问题描述】:
这是我第一次在这里堆栈溢出,所以很好:P。这是我应该向我的网络服务器发送一些信息的方法。
-(IBAction)updateEvent:(id)sender{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[[dict objectForKey:@"name"] addObject:_getNameLabel.text];
static NSString *url = @"http://localhost:5000/hello";
NSError *error;
NSData *event = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
if (! event) {
NSLog(@"Got an error: %@", error);
} else {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:event];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[event length]] forHTTPHeaderField:@"Content-Length"];
[request setURL:[NSURL URLWithString:url]];
}
// [gameInfoObject postEventInfo:event];
}
这是“应该”处理帖子消息的代码。我要做的就是获取该信息并将其写入现有文件。
import pickle
from flask import Flask
import json
@app.route('/hello/' , methods=['POST'])
def test():
with open("text.txt", "w") as text_file: #to check that it even gets here but it does not write anything at all to the file
text_file.write("It came here!!")
data = request.data
dataDict = json.loads(data)
with open('gameFile.txt', 'wb') as handle:
pickle.dump(dataDict, handle)
if __name__ == "__main__":
app.run()
所以当我运行它时,我没有得到任何错误,但我也没有得到任何结果。该文件仍然是空的,我在这里缺少什么?感谢我能得到的所有帮助!
【问题讨论】:
-
在
def test方法的末尾尝试return json.dumps({'msg': 'success'})。 -
我在哪里可以看到该消息? @SyedHabibM
-
在您的
objective-c代码响应中?! -
不,我什么也得不到。我编辑了我的问题,我在 python 代码中放了另一行来检查它是否到达那里,但似乎它没有......@SyedHabibM
标签: python objective-c flask request http-post