我想創建一個XML字符串並將其傳遞給另一個方法,我使用HTTP POST將它發送給PHP腳本。看到下面的日誌文件,我可以創建XML字符串並將其顯示在日誌文件中,但我無法將其傳遞給postXMLFeed方法。傳遞變量的問題 - Objective-C
我在做什麼錯?
-(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];
}
日誌文件:
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:
來自http://stackoverflow.com/faq: 當您決定哪個答案對您最有幫助時,通過單擊答案左側的複選框大綱將其標記爲接受的答案。這讓其他人知道你已經收到了你的問題的一個很好的答案。這樣做很有幫助,因爲它向其他人表明您從社區中獲得價值。 (如果你不這樣做,人們會經常禮貌地要求你回去接受更多你的問題的答案!) – falconcreek 2010-06-25 15:50:18