我想知道为什么这个HTTP POST请求不能在我的iPhone应用程序中工作。HTTP POST请求不起作用,委托方法不被调用
我知道这个URL是正确的,而且我发送的变量是正确的,但由于某种原因,请求没有被.aspx
页面收到。
编辑:
我重构代码到自己的类,有自己的委托方法。但委托方法没有被调用。
类被称为是这样的:
URLCallClass *reporter=[[[URLCallClass alloc] init]autorelease];
[reporter sendoview:@"http://mysite/page.aspx" params:httpBodyString];
,这是实际的类本身:
-(void)sendview:(NSString *)server params:(NSString *)params
{
NSURL* url=[[NSURL alloc] initWithString:server];
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
[url release];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
connectionResponse=[[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
//NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (!connectionResponse)
{
NSLog(@"Failed to submit request");
}
else
{
NSLog(@"---------Report Request submitted ---------");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"report received response");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"report failed with error");
NSLog(@"%@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Promo View Reported");
}
-(void)dealloc
{
[connectionResponse release];
[super dealloc];
}
所以澄清:所有这些代码运行在主线程以外的线程? – JeremyP 2010-05-18 15:35:06
那么在主线程中有UIView正在运行,并且httprequest代码是在此UIView中的类的实例中。我相信这个http请求类在完成它的异步urlconnection之前退出,一旦调用它的父UIView函数完成执行调用它的方法。 所以我不认为它是在主线程上,但叫它的类是。 虽然我可以离开这里,但对我来说这是相当新的,我仍然在学习。 – dubbeat 2010-05-19 08:44:16