我正在尝试将数据从iPhone同步到服务器,但没有得到它。我在过去四天之前就这样做了,但现在没有取得任何成功。它显示
我的服务器是在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);
}
我希望尽快得到帮助 谢谢
答案 0 :(得分:0)
这里有两个问题。
(1)超时 - 看起来你没有与192.168.9.68:91中的任何东西联系 - @ Mahesh的评论是正确的;在模拟器浏览器的Safari中检查它,检查是否可以连接到该机器。
(2)这是更严重的一个。
您正在为sqlite结果集中的每一行调用[self sendRequest]
。但是,您的所有requets都共享相同的webData
对象:(
您需要创建一个包含sendRequest
和webData
的单独类,并为您要发送的每个请求创建一个实例。