tags:

views:

44

answers:

2

Hello,

I'm trying to create an XML string and pass it to another method where I use the HTTP POST to send it to a PHP script. See the log file below, I can create the XML string and display it in the log file, but I cannot pass it to the postXMLFeed method.

What am I doing wrong ?

-(IBAction)syncUp { 
 [self createXMLFeed];
// [self postXMLFeed:XMLStr];
 [self postXMLFeed];
}


-(void)createXMLFeed{
 //Fetch details from the database.
 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tabrss" inManagedObjectContext:managedObjectContext];
 [request setEntity:entity];
 NSError *error;
 self.stories = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
 [request release];

 // Count the number of items in the array and display in the log.
 int arrayItemQuantity = [stories count];
 NSLog(@"Array Quantity: %d", arrayItemQuantity);

 // Loop through the array and display the contents in the log.
 int i;
 for (i = 0; i < arrayItemQuantity; i++)
 {
  //NSString *XMLStr = [NSString stringWithFormat:@"<workout><id>%@</id><userid>%@</userid><walkid>%@</walkid><date>%@</date><distance>%@</distance><repeats>%@</repeats><type>%@</type><intensity>%@</intensity><comments>%@</comments><time>%@</time><weight>%@</weight><height>%@</height></workout>", 
  XMLStr = [NSString stringWithFormat:@"<workout><id>%@</id><userid>%@</userid><walkid>%@</walkid><date>%@</date><distance>%@</distance><repeats>%@</repeats><type>%@</type><intensity>%@</intensity><comments>%@</comments><time>%@</time><weight>%@</weight><height>%@</height></workout>", 
       [[stories objectAtIndex:i] valueForKey:@"ID"], 
       [[stories objectAtIndex:i] valueForKey:@"UserID"], 
       [[stories objectAtIndex:i] valueForKey:@"walkID"], 
       [[stories objectAtIndex:i] valueForKey:@"xDate"], 
       [[stories objectAtIndex:i] valueForKey:@"Distance"], 
       [[stories objectAtIndex:i] valueForKey:@"Repeats"], 
       [[stories objectAtIndex:i] valueForKey:@"Type"],
       [[stories objectAtIndex:i] valueForKey:@"Intensity"],
       [[stories objectAtIndex:i] valueForKey:@"Comments"],
       [[stories objectAtIndex:i] valueForKey:@"xTime"],
       [[stories objectAtIndex:i] valueForKey:@"Weight"],
       [[stories objectAtIndex:i] valueForKey:@"Height"]];

  NSLog (@"XML Feed1: %@", XMLStr); 
 }
 NSLog (@"XML Feed2: ", XMLStr);

 [stories release];  

 // Update log file.
 NSLog(@"Database read and XML feed created."); 
}


//-(void)postXMLFeed:(NSString *)XMLStr
-(void)postXMLFeed
{ 
 //XMLStr = @"<workout><id>1</id><userid>4</userid><walkid>20</walkid><date>2010-06-21 10:42:50</date><distance>12345</distance><repeats>1</repeats><type>StephenTesting</type><intensity>6.0</intensity><comments>testtesttest</comments><time>00:00:00</time><weight>65</weight><height>173</height></workout>";
 //XMLStr = @"<xml><id>Hello World!</id></xml>";
 NSLog (@"XML Feed3: ", XMLStr);

 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200/stephen/sync_upload.php"];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
 [request setHTTPMethod:@"POST"];
 [request setHTTPBody:[XMLStr dataUsingEncoding:NSASCIIStringEncoding]];

 NSURLResponse *response;
 NSError *error;
 response = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


 // Update log file.
 NSLog(@"XML feed POSTED to website: ", XMLStr);

 //[super postXMLFeed];
}

Log file:

2010-06-25 14:34:38.001 TAB RSS[2229:207] XML Feed: <workout><id>27
  </id><userid>4
  </userid><walkid>
  </walkid><date>2010-06-21 10:42:25
  </date><distance>1000
  </distance><repeats>1
  </repeats><type>Walking
  </type><intensity>6.0
  </intensity><comments>asdf
  </comments><time>00:00:00
  </time><weight>65
  </weight><height>173
</height></workout>
2010-06-25 14:34:38.001 TAB RSS[2229:207] XML Feed: <workout><id>28
  </id><userid>4
  </userid><walkid>
  </walkid><date>2010-06-21 10:45:05
  </date><distance>152.4
  </distance><repeats>1
  </repeats><type>Hiking/Backpacking
  </type><intensity>8.0
  </intensity><comments>test
  </comments><time>00:00:00
  </time><weight>65
  </weight><height>173
</height></workout>
2010-06-25 14:34:38.002 TAB RSS[2229:207] XML Feed: <workout><id>29
  </id><userid>4
  </userid><walkid>
  </walkid><date>2010-06-21 12:31:28
  </date><distance>0
  </distance><repeats>1
  </repeats><type>Aikido
  </type><intensity>5.0
  </intensity><comments>Nice training
  </comments><time>01:30:00
  </time><weight>65
  </weight><height>173
</height></workout>
2010-06-25 14:34:38.002 TAB RSS[2229:207] XML Feed2: 
2010-06-25 14:34:38.003 TAB RSS[2229:207] Database read and XML feed created.
2010-06-25 14:34:38.004 TAB RSS[2229:207] XML Feed3: 
(gdb) continue
Current language:  auto; currently objective-c
2010-06-25 14:34:39.585 TAB RSS[2229:207] XML feed POSTED to website: 
A: 

stringWithFormat: is an NSString class method. It doesn't retain the string it returns, so it only persists for the remainder of the scope in which it was called. If you were to use [[NSString alloc] initWithFormat:] instead, or simply call retain on your XMLStr variable, you should be able to use it just fine in the second method. Just remember to release the string when you're done with it at the end of the second method.

Endemic
got it working thanks.
Stephen
A: 

You are assigning XMLStr to an autorealeased NSString I will assume that XMLStr is declared as NSString property as follows:

@interface XMLWriter : NSObject {
NSString *XMLStr;
}
@property (nonatomic, retain) NSString *XMLStr
@end

@implementation XMLWriter
@synthesize XMLStr;

In createXMLFeed change:

XMLStr = [NSString ....

To:

self.XMLStr = [NSString ....
falconcreek
yes, got it....thanks
Stephen