0

我想从 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];

提前致谢

4

2 回答 2

1

任何 PHP 脚本都可以从NSURLConnection. 这是一个如何做到这一点的示例:

PHP

<?php

    print json_encode(array('MyKey' => 'value123'));
?>

Objective-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);
}

随意将同步请求更改为使用委托进行异步调用。

于 2013-01-22T04:44:25.833 回答
0

解决方案取决于您的方便。那就是您的 Web 服务是 JSON 格式或 XML 格式,取决于您可以解析并使用响应。

于 2013-01-22T05:07:35.517 回答