我正在尝试创建一个XML字符串并将其传递给另一个我使用HTTP POST将其发送到PHP脚本的方法。请参阅下面的日志文件,我可以创建XML字符串并将其显示在日志文件中,但我无法将其传递给postXMLFeed方法。
我做错了什么?
-(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:
答案 0 :(得分:0)
stringWithFormat:
是一个NSString类方法。它不保留它返回的字符串,因此它只会在调用它的作用域的剩余部分中保留。如果您改为使用[[NSString alloc] initWithFormat:]
,或者只是在XMLStr变量上调用retain
,那么您应该可以在第二种方法中使用它。在第二种方法结束时完成它后,请记住release
字符串。
答案 1 :(得分:0)
您要将XMLStr
分配给自动审核的NSString
我将假设XMLStr
被声明为NSString
属性,如下所示:
@interface XMLWriter : NSObject {
NSString *XMLStr;
}
@property (nonatomic, retain) NSString *XMLStr
@end
@implementation XMLWriter
@synthesize XMLStr;
在createXMLFeed
更改:
XMLStr = [NSString ....
要:
self.XMLStr = [NSString ....