Getting sync(uploading) multipale data on the remote server from iphone
Posted
by
Ajay
on Stack Overflow
See other posts from Stack Overflow
or by Ajay
Published on 2011-09-20T08:43:15Z
Indexed on
2011/11/25
9:50 UTC
Read the original article
Hit count: 192
iphone
|objective-c
i am try to sync or upload the data on the remote server from iphone but not getting it.I try this from 1 week but didn't success how can solve this .I am using the NSURLConnection methods or any one give idea on ASIHTTPRequest method but i am new for *ASIHTTPReques*t .I need this method only .For this code like this
-(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);
}
it is not going into the this mthods
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",loginStatus);
}
It show me html page on console
I hope people help me to solve it out
© Stack Overflow or respective owner