没有从NSMutableURLRequest(iPhone应用程序同步)接收数据

时间:2012-01-10 20:36:23

标签: iphone objective-c ios

过去,我创建了一个iPhone应用程序,它依赖于php页面更新的sqlite数据库。当您访问PHP页面时,它会打印出一个sqlite数据库的转储,该应用程序会运行各种查询来更新sqlite数据库。我以前的应用程序以这种方式运行良好,并会相应更新。我最近尝试将相同的流程应用于具有更大数据库的新应用程序。我遇到了问题。

我正在做的是将一些帖子变量发送到PHP页面,该页面识别POST并回显sqlite命令以更新数据库。问题是页面打印命令需要几秒钟。如果我有页面打印一些静态文本,我在NSLog中得到它(@“响应数据将是==>%@”,结果);命令。如果我必须从我的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);

1 个答案:

答案 0 :(得分:0)

尝试理解这段代码..这有助于你。

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];

// [activityIndi​​cator startAnimating];

if ( theConnection )

{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}