NSMutableData 响应字符串
我正在尝试将图像上传到 yFrog,它工作得很好,但我只想从响应中获取 URL。当我使用 NSURLConnection 方法时,
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString* responseString = [[NSString alloc] initWithData:webData
encoding:NSUTF8StringEncoding];
// NSString *url = [webData valueForKey:@"mediaurl"];
NSLog(@"result: %@", responseString);
}
我将其作为响应字符串
result: <?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
<mediaid>hszuhsp</mediaid>
<mediaurl>http://yfrog.com/hszuhsp</mediaurl>
</rsp>
正如您在我的屏蔽代码中看到的那样,我尝试为我的 NSMutableData 提供键的值@“mediaurl”,它崩溃了。我认为这应该相对容易,但由于某种原因,我就是不知道如何从响应中获取 URL。任何帮助将不胜感激。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是有效的 xml 响应。
如果您只需要解析此响应,则可以迭代
NSString
。但是,如果您有 xml 格式的其他响应,那么最好的方法是使用任何 XML 解析器来解析它。:)
这里是如何选择您的 xml 解析器,然后您将相应地搜索教程。
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.
您不能在
NSMutableData
类的实例上使用valueForKey
,您应该使用 XML 解析器来提取值。you can't use the
valueForKey
on the instance ofNSMutableData
class., you should use the XML parser to extract the value.