如何检测HTTP响应、解析xml并保存到NSString?

时间:2021-07-21 23:00:07

I've been trying to figure this out for weeks and i still get nothing. I am using the ASIHTTPRequest and ive successfully sent the data to a server and now I need to get the response XML,parse it and save the elements to each labeled NSString so I can post it to the server. Does anyone have an idea on how to do this?

我已经想了好几个星期了,但还是一无所获。我正在使用ASIHTTPRequest和ive成功地将数据发送到服务器,现在我需要获得响应XML,解析它并将元素保存到每个标记的NSString中,这样我就可以将它发送到服务器。有人知道怎么做吗?

2 个解决方案

#1


4  

From looking at the How to Use page, I think what you want to do is implement methods that can be called when the request is complete. For example, say you have a method done: that you want to be called when your request completes. You can set that method as your "finished" selector on the request:

从查看如何使用页面,我认为您要做的是实现在请求完成时可以调用的方法。例如,假设您已经完成了一个方法:您希望在请求完成时调用该方法。您可以将该方法设置为您的“已完成”选择器:

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setDelegate:self];
[request setDidFinishSelector:@selector(done:)];

Then later, you implement the done: method:

然后,实现done:方法:

- (void)done:(ASIHTTPRequest *)request
{
   NSString *response = [request responseString];
}

This is all assuming you're sending the requests asynchronously; if you're using synchronous calls, you can just use the responseString property on the request.

这一切都假设您正在异步发送请求;如果您正在使用同步调用,您可以在请求上使用responseString属性。

#2


2  

Get a copy of an XML library for the iPhone if you have other than trivial XML parsing needs.
I've used Google's GDataXMLNode for this before, but I would probably use KissXML for future work, because it's nearer NSXML (the Apple library which doesn't ship for the iphone unfortunately).

如果您除了需要简单的XML解析外,还需要为iPhone获取一个XML库的副本。我以前使用过谷歌的GDataXMLNode,但以后可能会使用KissXML进行工作,因为它更接近NSXML(不幸的是,苹果的库没有为iphone提供)。

Here's a way to parse out the response, here I'm looking for errors you might typically see from a Rails server, along the lines of:

这里有一种解析响应的方法,我在这里查找您通常在Rails服务器上看到的错误,如:

  <errors><error>Description of what went wrong</error></errors>

You can see that I get the 'request' object back from the library, and I feed the response string into a GDataXMLDocument.

您可以看到,我从库中获取了“请求”对象,并将响应字符串输入到GDataXMLDocument中。

   GDataXMLDocument* root = [[GDataXMLDocument alloc] initWithXMLString:[request responseString] options:0 error:nil];

   // Parse the error strings
   NSArray* errors = [root nodesForXPath:@"//errors/error" error:nil];

   // What is the first error string.... etc......
   NSString* firstError = [[errors objectAtIndex:0] stringValue];

The returned array here contains a list of nodes that match that path in the XML. If you're not familiar with XPath, it's not hard to learn, and useful for pulling data out of an XML response. Calling stringValue against the element returned in the array will return the text between the elements in the example above.

这里返回的数组包含与XML中的路径匹配的节点列表。如果您不熟悉XPath,那么学习它并不困难,并且对于从XML响应中提取数据非常有用。对数组中返回的元素调用stringValue将返回上面示例中元素之间的文本。

You can of course populate a dictionary, etc. with the returned XML data. Make sense?

当然,您可以用返回的XML数据填充字典等。有意义吗?

#1


4  

From looking at the How to Use page, I think what you want to do is implement methods that can be called when the request is complete. For example, say you have a method done: that you want to be called when your request completes. You can set that method as your "finished" selector on the request:

从查看如何使用页面,我认为您要做的是实现在请求完成时可以调用的方法。例如,假设您已经完成了一个方法:您希望在请求完成时调用该方法。您可以将该方法设置为您的“已完成”选择器:

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setDelegate:self];
[request setDidFinishSelector:@selector(done:)];

Then later, you implement the done: method:

然后,实现done:方法:

- (void)done:(ASIHTTPRequest *)request
{
   NSString *response = [request responseString];
}

This is all assuming you're sending the requests asynchronously; if you're using synchronous calls, you can just use the responseString property on the request.

这一切都假设您正在异步发送请求;如果您正在使用同步调用,您可以在请求上使用responseString属性。

#2


2  

Get a copy of an XML library for the iPhone if you have other than trivial XML parsing needs.
I've used Google's GDataXMLNode for this before, but I would probably use KissXML for future work, because it's nearer NSXML (the Apple library which doesn't ship for the iphone unfortunately).

如果您除了需要简单的XML解析外,还需要为iPhone获取一个XML库的副本。我以前使用过谷歌的GDataXMLNode,但以后可能会使用KissXML进行工作,因为它更接近NSXML(不幸的是,苹果的库没有为iphone提供)。

Here's a way to parse out the response, here I'm looking for errors you might typically see from a Rails server, along the lines of:

这里有一种解析响应的方法,我在这里查找您通常在Rails服务器上看到的错误,如:

  <errors><error>Description of what went wrong</error></errors>

You can see that I get the 'request' object back from the library, and I feed the response string into a GDataXMLDocument.

您可以看到,我从库中获取了“请求”对象,并将响应字符串输入到GDataXMLDocument中。

   GDataXMLDocument* root = [[GDataXMLDocument alloc] initWithXMLString:[request responseString] options:0 error:nil];

   // Parse the error strings
   NSArray* errors = [root nodesForXPath:@"//errors/error" error:nil];

   // What is the first error string.... etc......
   NSString* firstError = [[errors objectAtIndex:0] stringValue];

The returned array here contains a list of nodes that match that path in the XML. If you're not familiar with XPath, it's not hard to learn, and useful for pulling data out of an XML response. Calling stringValue against the element returned in the array will return the text between the elements in the example above.

这里返回的数组包含与XML中的路径匹配的节点列表。如果您不熟悉XPath,那么学习它并不困难,并且对于从XML响应中提取数据非常有用。对数组中返回的元素调用stringValue将返回上面示例中元素之间的文本。

You can of course populate a dictionary, etc. with the returned XML data. Make sense?

当然,您可以用返回的XML数据填充字典等。有意义吗?