从 iPhone 同步服务器上的数据时出现超时问题
Posted
技术标签:
【中文标题】从 iPhone 同步服务器上的数据时出现超时问题【英文标题】:Having time out problem at time sync the data on the server from iPhone 【发布时间】:2011-09-07 11:01:04 【问题描述】:我正在尝试将数据从 iPhone 同步到服务器,但没有得到它。在过去的四天里,我曾经这样做过,但现在没有任何成功。它显示
-
** 连接失败!错误 - 请求超时。
http://192.168.0.68:91/JMAPI?RequestType=User&Command=NEW
** -[NSConcreteMutableData release]:消息发送到 deallocated
实例 0x165590a0
我的服务器是在 C# 的帮助下创建的 我有这样的本地服务器链接http://192.168.0.68:91/JMAPI 为了发布我这样编码的数据
-(void)sendRequest
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
NSString *post = [NSString stringWithFormat:@"Token=%@&JourneyID=%@&JourneyName=%@&Locationname=%@&StartDate=%@&Distance=%@&ShareType=%@&LastSyncDate=%@",tokenapi,Journey_ID,Journey_Name,Location_Name,Start_Date,Dist,Share_type,str];
NSLog(@"%@",post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"%@",postLength);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]autorelease];
[request setURL:[NSURL URLWithString:@"http://192.168.0.68:91/JMAPI?RequestType=User&Command=NEW"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
webData = [[NSMutableData data] retain];
NSLog(@"%@",webData);
/// this for checking is that Sync is work or not
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
[webData setLength: 0];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[webData appendData:data];
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
[connection release];
[webData release];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
***//ABove line i am getting error that Connection failed! Error - The request timed out.
http://192.168.0.68:91/JMAPI?RequestType=User&Command=NEW***
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",loginStatus);
[loginStatus release];
[connection release];
[webData release];
And i am getting this data from sqlitedatabase and from here i am calling the -(void)sendRequest Which is containing posting method . For code like this
-(void)information
NSString *filePath = [self getWritableDBPath];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
NSString *qu = @"Select JourneyID,JourneyName,Locationname,StartDate,Distance,ShareType FROM UserJourney where SyncStatus ='1'";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [qu UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
while(sqlite3_step(compiledStatement) == SQLITE_ROW )
char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);
Journey_ID= [[NSString alloc]initWithUTF8String:firstColumn];
NSLog(@"%@",Journey_ID);
char * scondColumn = (char *)sqlite3_column_text(compiledStatement, 1);
Journey_Name = [[NSString alloc]initWithUTF8String:scondColumn];
NSLog(@"%@",Journey_Name);
char *thridColumn = (char *)sqlite3_column_text(compiledStatement, 2);
Location_Name =[[NSString alloc]initWithUTF8String:thridColumn];
NSLog(@"%@",Location_Name);
char *fuothColumn = (char *)sqlite3_column_text(compiledStatement, 3);
Start_Date = [[NSString alloc]initWithUTF8String:fuothColumn];
NSLog(@"%@",Start_Date);
Dist = [NSNumber numberWithFloat:(float)sqlite3_column_double(compiledStatement, 4)];
NSLog(@"%f",Dist);
Share_type = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 5)];
NSLog(@"%f",Share_type);
NSLog(@"%@,%@,%@,%@,%@",Journey_ID,Journey_Name,Location_Name,Start_Date,Dist,Share_type);
[self sendRequest];***// this is place from where I call the posting method(sendRequest)***
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
希望尽快得到帮助 谢谢
【问题讨论】:
打开 NSZombie 并通过 Instruments 进行测试。它将帮助您找到双重释放的对象。 从浏览器检查 URL 192.168.0.68:91/JMAPI?RequestType=User&Command=NEW 是否正常工作或超时。 【参考方案1】:这里有两个问题。
(1) 超时 - 看起来你没有连接到 192.168.9.68:91 的任何东西 - @Mahesh 的评论是正确的;在模拟器浏览器的 Safari 中检查它,以检查您是否可以连接到该机器。
(2) 这个比较严重。
您正在为 sqlite 结果集中的每一行调用[self sendRequest]
。但是,您所有的请求都共享同一个 webData
对象:(
您需要创建一个包含sendRequest
和webData
的单独类,并为您要发送的每个请求创建一个实例。
【讨论】:
我如何发送每个数据的请求。它会使我的程序变慢吗。如果让洞数据从同一页面发送到服务器会有什么不同 (1) 让每个数据都有自己的'webData'对象,否则发送的每一位数据的响应将相互覆盖。 (2) 否 - NSURLConnection 足够聪明,不会建立太多连接,并且大部分时间都花在等待服务器响应的后台线程中。 (3) 您可以一次性发送所有数据 - 这样会更快(需要建立的连接更少)并且可以修复您的“webData”错误 - 但是,在服务器上要做的工作更多。以上是关于从 iPhone 同步服务器上的数据时出现超时问题的主要内容,如果未能解决你的问题,请参考以下文章
处理 ClientInsertServerInsert 同步冲突时出现 SQL 命令“超时”异常
错误:从 XCODE 4.2 同步 .ipa 文件临时分发时出现 0xe8003ffe