views:

272

answers:

2

Hi there, I just wanted to ask you if anyone can help me parsing the returned data from the Twitpic API?

I'm creating a HTTPFormRequest using the ASIHTTPRequest Wrapper for Cocoa. This all happens in an iPhone application:

    NSURL *url = [NSURL URLWithString:@"http://twitpic.com/api/upload"];
NSString *username = t_user;
NSString *password = t_pass;
NSData *twitpicImage = UIImagePNGRepresentation(imageView.image);

// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];

[request setPostValue:twitpicImage forKey:@"media"];
[request setPostValue:username forKey:@"username"];
[request setPostValue:password forKey:@"password"];
[request setData:twitpicImage forKey:@"media"];

// Initiate the WebService request
[request start];
if ([request error]) {

    NSLog(@"%@", [request error]);

} else if ([request responseString]) {

    NSLog(@"%@", [request responseString]);

}}

Now comes the hard part, I don't know how to parse the data that is in [request responseString]. I know I need to use NSXMLParser, but I dunno how to use it. All I need is to get the url of the image.

Thx in advance.

A: 

Feel free to have a look at my little XML parse classes here http://www.memention.com/blog/2009/10/31/The-XML-Runner.html

I have started to use them for parsing the response from image upload to yfrog.com

Basically I do like this...

In NameValueParser.m I changed the entry tag to rsp like this

entryName = [[NSString stringWithString:@"rsp"] retain];

then where the response has been received I parse it like this

NameValueParser *parser = [NameValueParser parser];
[parser addFieldName:@"statusid"];
[parser addFieldName:@"userid"];
[parser addFieldName:@"mediaid"];
[parser addFieldName:@"mediaurl"];
[parser addFieldName:@"err"];
[parser parseData:responseData]; // the response received by ASIHTTPRequest

NSArray *rspArray = [parser list];

NSLog(@"%@", rspArray); // Have a look at it here
epatel
A: 

Try it as written at the bottom of this tutorial click here using NSScanner. They are showing exactly what you need, retrieving only the mediaurl = URL of uploaded image.

NSScanner *scanner = [NSScanner scannerWithString:responseString]; ...
jd291