Iphone HTTP POST请求不工作,未调用委托方法
我想知道为什么这个HTTP POST请求在我的iPhone应用程序中不起作用 我知道URL是正确的,我发送的变量也是正确的,但是由于某种原因,Iphone HTTP POST请求不工作,未调用委托方法,iphone,http,nsurlconnection,Iphone,Http,Nsurlconnection,我想知道为什么这个HTTP POST请求在我的iPhone应用程序中不起作用 我知道URL是正确的,我发送的变量也是正确的,但是由于某种原因,.aspx页面没有收到请求 编辑: 我用自己的委托方法将代码重构为自己的类。但是没有调用委托方法 该类的名称如下: URLCallClass *reporter=[[[URLCallClass alloc] init]autorelease]; [reporter sendoview:@"http://mysite/page.aspx" param
.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];
}
您应该在委托中实现connection:didFailWithError:
NSLog(@“%@,[错误描述])
会告诉你到底哪里出了问题。你的url
真的被创建了吗?如果有任何问题,NSURLsinitWithString:
方法将返回nil
是否打印响应标题?我建议您实现几乎所有委托方法来检查连接的实际情况,首先尝试打印didReceiveResponse方法,以及使用connection:didReceiveData方法接收的一些数据
可能服务器无法处理数据的编码,请尝试使用NSUTF8StringEncoding,而不是NSISolatin1StringCodeing作为正文编码。这与线程有关 我使用了这个解决方案
didfailWithError处理程序从未被调用。我注释掉了我所有的代码,但有问题的代码除外,而且它可以找到。我在同一个类中使用另一个NSURLConnection。它在这之前被执行。这可能是造成问题的原因吗?url正在成功创建以便澄清:所有这些代码都在主线程以外的线程上运行?主线程上运行UIView,httprequest代码位于该UIView中的类实例中。我相信,一旦调用它的父UIView函数执行完调用它的方法,这个http请求类在完成其异步urlconnection之前就退出了。所以我不认为它在主线程上,但是调用它的类是。虽然我可能离这里很远,但这对我来说还是很新鲜,我还在学习。