我想从iphone中的特定URL进行简单的XML调用,但是在调用时我没有每次都获得更新的结果,我已经检查了URL但仍然没有得到实际的结果,
听到.m控制器代码
//This is SOAP calling
NSString *postString = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<GetCountries xmlns=\"http://www.webserviceX.NET\"/>\n"
"</soap:Body>\n"
"</soap:Envelope>";
//this is calling from URL wia above SOAP
dataWebService = [[NSMutableData data] retain];
NSURL *tempURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.webservicex.net/country.asmx"]];
NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:tempURL] retain];
NSString *postLength = [NSString stringWithFormat:@"%d", [postString length]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://www.webserviceX.NET/GetCountries" forHTTPHeaderField:@"SOAPAction"];
[request addValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection * myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
NSLog(@"%@Testing ",myConnection);
[request release];
提前致谢
答案 0 :(得分:1)
可以从NSURLConnection
调用任何PHP脚本。以下是如何做到这一点的示例:
PHP
<?php
print json_encode(array('MyKey' => 'value123'));
?>
目标-C
NSData *myReceivedData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myurl/path/to/api"]]];
NSError *jsonParseError = nil;
NSDictionary *jsonObj = [NSJSONSerialization JSONObjectWithData:myReceivedData options:0 error:&jsonParseError];
if (jsonParseError == nil) {
NSLog(@"Value of MyKey: %@", [jsonObj objectForKey:@"MyKey"]);
}
else {
NSLog(@"JSON Parse error: %@", jsonParseError);
}
随意将同步请求更改为使用委托进行异步调用。
答案 1 :(得分:0)
解决方法取决于您的便利性。 那就是您的Web服务是JSON格式或XML格式,取决于您可以解析它并使用响应。