我有一个iOS应用程序,它使用NSURL将数据发送到服务器,在Python中检索它。这是我在Xcode中的相关NSURL代码:
NSString* base=@"http://gpselftracker.appspot.com/scan";
NSURL* URL=[NSURL URLWithString:base];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";
NSString *postString = @"?latitude=";
NSString* postString1=[postString stringByAppendingString:latitude1];
NSString* postString2=[postString1 stringByAppendingString:@"&longitude="];
NSString* postString3=[postString2 stringByAppendingString: longitude1];
NSString* postString4=[postString3 stringByAppendingString:@"&altitude="];
NSString* postString5=[postString4 stringByAppendingString: altitude1];
NSString* postString6=[postString5 stringByAppendingString:@"&speed="];
NSString* postString7=[postString6 stringByAppendingString: speed1];
NSString* postString8=[postString7 stringByAppendingString:@"&heading="];
NSString* postString9=[postString8 stringByAppendingString:heading1];
NSString* params=[base stringByAppendingString:postString9];
NSData *data = [params dataUsingEncoding:NSUTF8StringEncoding];
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%i", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
以下是服务器上检索值的结果:
def post(self):
params=list()
session=get_current_session()
latitude=self.request.POST["latitude"]
longitude=self.request.POST["longitude"]
altitude=self.request.POST["altitude"]
speed=self.request.POST["speed"]
heading=self.request.POST["heading"]
我意识到一些代码,特别是Obj-C中的连接,是一种令人讨厌的问题,但几个月前我就有了这个工作,丢失了源代码,并且我试图从头开始重新创建。当我运行它时,我在GAE日志控制台中得到的错误是:
'latitude'
Traceback (most recent call last):
File "/base/data/home/runtimes/python/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 716, in __call__
handler.post(*groups)
File "/base/data/home/apps/s~gpselftracker/1.372388516394179216/main.py", line 43, in post
latitude=self.request.POST["latitude"]
File "/base/data/home/runtimes/python/python_lib/versions/1/webob/multidict.py", line 302, in __getitem__
return self._decode_value(self.multi.__getitem__(key))
File "/base/data/home/runtimes/python/python_lib/versions/1/webob/multidict.py", line 77, in __getitem__
raise KeyError(key)
KeyError:'latitude'
答案 0 :(得分:0)
尝试这个。
NSMutableString* postString9 = [base mutableCopy];//i dont know if you want to change the base string so I decided not to in this example.
[postString stringByAppendingString: @"?latitude="];
[postString stringByAppendingString:latitude1];
[postString9 stringByAppendingString:@"&longitude="];
[postString9 stringByAppendingString: longitude1];
[postString9 stringByAppendingString:@"&altitude="];
[postString9 stringByAppendingString: altitude1];
[postString9 stringByAppendingString:@"&speed="];
[postString9 stringByAppendingString: speed1];
[postString9 stringByAppendingString:@"&heading="];
[postString9 stringByAppendingString:heading1];
postString9= (NSMutableString*)[urlsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *data= [postString9 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:base]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:20];
[request setHTTPMethod: @"POST"];
[request addValue:[NSString stringWithFormat:@"%i", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:data];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
我希望这会对你有所帮助。