2010-06-25 7 views
0

J'essaie de créer une chaîne XML et de la passer à une autre méthode dans laquelle j'utilise HTTP POST pour l'envoyer à un script PHP. Voir le fichier journal ci-dessous, je peux créer la chaîne XML et l'afficher dans le fichier journal, mais je ne peux pas le passer à la méthode postXMLFeed.Problème lors de la transmission des variables - Objective-C

Qu'est-ce que je fais mal?

-(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]; 
} 

fichier journal:

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: 
+0

Sur le http://stackoverflow.com/faq: Lorsque vous avez décidé quelle réponse vous était la plus utile, marquez-la comme la réponse acceptée en cliquant sur le cadre de la case à cocher à gauche de la réponse. Cela permet aux autres personnes de savoir que vous avez reçu une bonne réponse à votre question. Cela est utile car cela montre aux autres que vous obtenez de la valeur de la communauté. (Si vous ne le faites pas, les gens vous demanderont souvent poliment de revenir en arrière et d'accepter des réponses pour plus de vos questions!) – falconcreek

Répondre

0

stringWithFormat: est une méthode de classe NSString. Il ne conserve pas la chaîne qu'il renvoie, il ne persiste donc que pour le reste de la portée dans laquelle il a été appelé. Si vous deviez utiliser [[NSString alloc] initWithFormat:] à la place, ou simplement appeler retain sur votre variable XMLStr, vous devriez pouvoir l'utiliser très bien dans la seconde méthode. Rappelez-vous simplement release la chaîne lorsque vous avez terminé avec elle à la fin de la deuxième méthode.

+0

eu le travail merci. – Stephen

0

Vous assignez XMLStr à un autorealeased NSString Je suppose que XMLStr est déclarée comme NSString propriété comme suit:

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

@implementation XMLWriter 
@synthesize XMLStr; 

En createXMLFeed changement:

XMLStr = [NSString .... 

Pour:

self.XMLStr = [NSString .... 
+0

oui, je l'ai .... merci – Stephen

Questions connexes