作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试从 iPhone 同步或上传远程服务器上的数据,但没有成功。我从 1 周开始尝试,但没有成功。
如何解决这个问题。我正在使用 NSURLConnection 方法或任何提供想法的方法ASIHTTPRequest 方法 但我是 ASIHTTPRequest 的新手。我只需要这个方法用于这个代码:
- (void)sendRequestforContent
{
//this for finding the date of sync on the server
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:@"dd-MMM-yyyy"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSMutableData *postBody = [NSMutableData data];
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *boundary = @"-------------------a9d8vyb89089dy70";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setHTTPMethod:@"POST"];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this is for TOKEN_API
[postBody appendData:[@"Content-disposition: form-data; name=\"Token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[tokenapi dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the CONTENT_ID
[postBody appendData:[@"Content-disposition: form-data; name=\"contentID\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[content_id dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the CONTENTTYPE_ID
[postBody appendData:[@"Content-disposition: form-data; name=\"contentTypeID\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *ContentTypeString = [NSString stringWithFormat:@"%d",content_type];
[postBody appendData:[ContentTypeString dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the CONTENT_Location_Id
[postBody appendData:[@"Content-disposition: form-data; name=\"contentLocationID\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[contenLocation_id dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this is for the User_Caption
[postBody appendData:[@"Content-disposition: form-data; name=\"userCaption\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[user_caption dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this is for the User_Comment
[postBody appendData:[@"Content-disposition: form-data; name=\"userComment\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[user_comment dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the Tags
[postBody appendData:[@"Content-disposition: form-data; name=\"tags\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[tag dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the Date_Record
[postBody appendData:[@"Content-disposition: form-data; name=\"dateRecorded\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[date_recorded dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the image_data
[postBody appendData:[[NSString stringWithFormat:@"Content-disposition: form-data; name=\"image_file\"; filename=\"%@\"\r\n",@"image.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:image];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the Share_type
[postBody appendData:[@"Content-disposition: form-data; name=\"shareType\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *ShareString = [NSString stringWithFormat:@"%d",share_type];
[postBody appendData:[ShareString dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the Views
[postBody appendData:[@"Content-disposition: form-data; name=\"views\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *ViewsString = [NSString stringWithFormat:@"%d",views];
[postBody appendData:[ViewsString dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the PLAY_time
[postBody appendData:[@"Content-disposition: form-data; name=\"playTime\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *TimeString = [NSString stringWithFormat:@"%d",play_time];
[postBody appendData:[TimeString dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//this for the Posted_By
[postBody appendData:[@"Content-disposition: form-data; name=\"postedBy\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[postred_by dataUsingEncoding:NSUTF8StringEncoding]];
//this for the AVG_Rating
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-disposition: form-data; name=\"avgRating\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *AvgString = [NSString stringWithFormat:@"%d",avg_rating];
[postBody appendData:[AvgString dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-disposition: form-data; name=\"LastSyncDate\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
NSData *shoutData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *returnString = [[NSString alloc] initWithData:shoutData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
它不会进入这个方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@", loginStatus);
}
它在控制台上显示 HTML 页面。
最佳答案
NSString *urlString = [[NSString alloc] initWithString:@"http://192.168.0.1:96/JourneyMapperAPI?RequestType=Content&Command=New"];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSMutableData *postBody;//Here comes your data that you have created in the question
NSString *msgLength = [[NSString alloc] initWithFormat:@"%d", [postBody length]];//use either [postBody length] or [postBody bytes], I am not sure about it
ASIFormDataRequest *serverRequest = [ASIFormDataRequest requestWithURL:url];
[serverRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
[serverRequest addRequestHeader:@"Content-Length" value:msgLength];
[serverRequest appendPostData:postBody];
[serverRequest setDelegate:self];
[serverRequest setDidFinishSelector:@selector(sendAlertsRequestDone:)];
[serverRequest setDidFailSelector:@selector(sendAlertsRequestWentWrong:)];
[serverRequest setUploadProgressDelegate:progressView];// here progressView is UIProgressbar
[serverRequest startAsynchronous];
[urlString release];
[url release];
[postBody release];
[msgLength release];
关于iphone - 从 iphone 同步(上传)远程服务器上的多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7482363/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!