过去,我创建了一个依赖于由 php 页面更新的 sqlite 数据库的 iPhone 应用程序。当您访问 PHP 页面时,它会打印出 sqlite 数据库的转储,并且该应用程序会运行各种查询来更新 sqlite 数据库。我以前的应用程序以这种方式运行良好,并会相应更新。我最近尝试将相同的过程应用于具有更大数据库的新应用程序。我遇到了问题。
我正在做的是将一些 post 变量发送到 PHP 页面,该页面可识别 POST 并回显 sqlite 命令以更新数据库。问题是页面打印命令需要几秒钟。如果我让页面打印一些静态文本,我会在我的 NSLog(@"Response data is going to be ==> %@", result) 中得到它;命令。如果我必须从我的 MySQL 数据库中提取信息,大约需要 3-5 秒然后打印(在我的普通浏览器上)。然而,当我运行该应用程序时,它说响应数据将是 ==> (null) 而不是命令。
任何人都可以阐明可能发生的事情吗?我知道当它不打印很多东西并且它是静态的时候这会起作用。由于某种原因,动态部分使其返回 null。
感谢帮助。
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlBase]];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:60.0];
NSData *post = [myParameters dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[post length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:post];
[request setHTTPBody:[myParameters dataUsingEncoding:NSUTF8StringEncoding]];
// set headers
NSString *contentType = [NSString stringWithFormat:@"text/plain"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse
error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response data is going to be ==> %@", result);
最佳答案
试着理解这段代码..它对你有帮助。
NSString *soapMessage=[NSString stringWithFormat:@" YOUR WEB SERVICE CODE",myTxt1.text,myTxt2.text];
NSLog(@"%@",soapMessage);
//-----------create connection
NSURL *url=[NSURL URLWithString:@"http://your local host webservice url"];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];
NSString *msgLength=[NSString stringWithFormat:@"%d",[soapMessage length]];
//------------发送请求
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/AutenticateUser" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
//[activityIndicator startAnimating];
if ( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
关于iphone - 未从 NSMutableURLRequest 接收数据(iPhone 应用程序同步),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810149/