gpt4 book ai didi

php - 如何查看从 Objective C cocoa 发送到 php 的 GET 请求的响应

转载 作者:行者123 更新时间:2023-12-03 17:52:53 26 4
gpt4 key购买 nike

我是 Objective C 和 Cocoa 框架的新手。我已经编写了以下代码并从 cocoa 向 php 发送请求,但无法从 php/server 获得响应,请帮忙。

NSString *fname=@"nam1";
NSString *lname=@"nam2";
NSString *urlString = [[NSString alloc] initWithFormat:@"http://url/send.php?
first_name=%@&last_name=%@",fname,lname];
NSLog(urlString);
NSURL *url = [ NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSAlert *dq= [[NSAlert alloc] init];
[dq setMessageText:urlString];
[dq runModal];
[request setHTTPMethod:@"GET"];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
NSAlert *d= [[NSAlert alloc] init];
[d setMessageText:@"connection"];
[d runModal];

对于响应,以下代码不起作用,我没有收到响应。

     - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
*)response
{
_responseData = [[NSMutableData alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable

[_responseData appendData:data];
NSLog(@"response string: %@", _responseData);
}

并响应这个 NSLog(@"response string: %@", _responseData);响应字符串:efbbbf3c 3f786d6c 20766572 73696f6e 3d22312e 30222065 6e636f64 696e673d 22757466 2d382220 3f3e0a3c 41727261 794f6646 697 26554 65787441 70694d73 673e0a3c 2f417272 61794f66 46697265 54657874 4170694d 73673e 出现在控制台中。

最佳答案

如果您只想将 NSData 作为 NSString 检查,您可以将以下内容放入 connectionDidFinishLoading: 方法中:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *string = [[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
NSLog(@"_responseData string = %@", string);
}

话虽如此,响应似乎是 XML:

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfFireTextApiMsg>
</ArrayOfFireTextApiMsg>

这是你所期待的吗?如果是这样,您可以使用NSXMLParser来解析它。

例如,假设您有一个包含一些数据的 XML 响应:

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfFireTextApiMsg>
<FireTextApiMsg>Message1</FireTextApiMsg>
<FireTextApiMsg>Message2</FireTextApiMsg>
<FireTextApiMsg>Message3</FireTextApiMsg>
</ArrayOfFireTextApiMsg>

connectionDidFinishLoading中,您可以执行以下操作:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:_responseData];
[parser setDelegate:self];
if ([parser parse])
NSLog(@"fireTextApiMsgs = %@", self.fireTextApiMsgs);
else
NSLog(@"parse failed");
}

显然,您需要声明您的类符合委托(delegate)协议(protocol)并定义一些属性来跟踪您解析的值:

@interface ViewController () <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableArray *fireTextApiMsgs;
@property (nonatomic, strong) NSMutableString *currentValue;

@end

你会实现 NSXMLParserDelegate方法,例如:

#pragma mark - NSXMLParserDelegate

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"ArrayOfFireTextApiMsg"])
self.fireTextApiMsgs = [NSMutableArray array];
else if ([elementName isEqualToString:@"FireTextApiMsg"])
self.currentValue = [NSMutableString string];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[self.currentValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"FireTextApiMsg"]) {
[self.fireTextApiMsgs addObject:self.currentValue];
self.currentValue = nil;
}
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"%s: %@", __FUNCTION__, parseError);
}

这将生成如下输出:

2013-12-20 12:08:55.167 XMLTestApp[12531:70b] fireTextApiMsgs = (    Message1,    Message2,    Message3)

显然,您的最终 XML 无疑会有所不同(我只是编了一个示例),因此您必须根据 XML 响应的外观以及您尝试从中解析的内容来调整代码,但是希望这能说明这个想法。

关于php - 如何查看从 Objective C cocoa 发送到 php 的 GET 请求的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com