I am trying to post the data from iphone for that use the code like this
-(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];
; ****i am getting the warning 'NSData' may not respond to '- autorelease];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(@"%@",webData);
}
}
And it get crash and show the -[NSConcreteMutableData setLength:]: message sent to deallocated instance 0xdde2760
this updata code
-(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];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(@"%@",webData);
}
}
-(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];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",loginStatus);
[log开发者_StackOverflow中文版inStatus release];
[connection release];
[webData release];
}
I think below code help you
NSString *soapMessage=postString;//Your string
NSData *data = [[NSData alloc] initWithData:[soapMessage dataUsingEncoding:NSASCIIStringEncoding]];
//NSLog(@"%@", data);
NSString *filename = @"filename";
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"YOUR-URL"]];
[theRequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[theRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data]];
[data release];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setHTTPBody:postbody];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection ){
myWebData=[[NSMutableData alloc] initWithLength:0];
}
You a having warning
there because you are assigning object of class NSString
to NSData
, look here:
NSData *post = [NSString stringWithFormat:@"Token=%@&JourneyID=%@&JourneyName=%@&Locationname=%@&StartDate=%@&Distance=%@&ShareType=%@&LastSyncDate=%@",tokenapi,Journey_ID,Journey_Name,Location_Name,Start_Date,Dist,Share_type,str];
So first of all try to replace it with
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];
You have set your NSMutableRequest to autorelease. This may be the reason it's crashing as it's being released before you want it to. Get rid of that and instead manually release it ([request release[) after your if(theConnection). This may fix the crash.
Have you set the functions for handling the connection ? As this isn't in your question ...
I think you need to take ownership of the object at this particular point.
postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]
using retain/copy properties...
Remember that you explicitly have to take ownership for returned objects from methods that contain neither new, alloc, retain or copy in their name as they return autoreleased instances.
Hope this will help you ...
Your first and most obvious problem is that you have decalred post to be of type NSData* but are assignining it to a NSString*.
Secondly you probably need to start the url connection at some point otherwise nothing will happen.
I strongly recommend using the ASI library for you http requests. It will save you a lot of time and effort.
As I said on the chat, you should do the submit with ASIFormDataRequest class instead of the NSMutableURLRequest, because it is much easier and it is well tested and it has a good documentation here.
精彩评论