NSMutableData response string

2019-08-02 05:43发布

I am trying to upload images to yFrog which is working just fine, but I want to grab just the URL from the response. When I use the NSURLConnection method

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];


NSString* responseString = [[NSString alloc] initWithData:webData  
encoding:NSUTF8StringEncoding];

// NSString *url = [webData valueForKey:@"mediaurl"];
NSLog(@"result: %@", responseString);
}

I get this as my response string

result: <?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
<mediaid>hszuhsp</mediaid>
<mediaurl>http://yfrog.com/hszuhsp</mediaurl>
</rsp> 

As you can see in my blocked out code I tried to give my NSMutableData to give me the value of the key @"mediaurl" which just crashes. I think this should be relatively easy but for some reason I just can not figure out how to just grab the URL out of the response. Any help would be greatly appreciated. Thank you

2条回答
家丑人穷心不美
2楼-- · 2019-08-02 06:24

This is a valid xml response.

If you need to parse only this response, you can iterate the NSString.

But if you have other response in xml format then the best approach is you are going to parse it using any XML parser.. :)

Here is how to choose your xml parser, and then you will search for tutorial accordingly.

查看更多
戒情不戒烟
3楼-- · 2019-08-02 06:34

you can't use the valueForKey on the instance of NSMutableData class., you should use the XML parser to extract the value.

查看更多
登录 后发表回答