【发布时间】:2013-12-30 12:20:36
【问题描述】:
我创建了一个 .net Web 服务,它接受一个参数(int DishId)并返回一个带有属性的 XML 文件:
<Dish xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Menu_Id>3</Menu_Id>
<Dish_Name>DNN</Dish_Name>
<Dish_Description>DD</Dish_Description>
<Dish_Price>1555</Dish_Price>
<Dish_Ingredients>DI</Dish_Ingredients>
<Dish_Picture>images/smile.png</Dish_Picture>
<Dish_Category>3</Dish_Category>
</Dish>
所以在搜索了如何在 iOS Xcode 5.0.2 中使用 Web 服务之后。我遵循了 SOAP 方法。我点击了这个链接:Consuming XML Web Services in iPhone Applications
我创建了一个新页面,其中包含标签、文本字段(“下面代码中的菜号”)、按钮、活动指示器。
所以对于按钮操作,我创建了以下代码:
- (IBAction)buttonClicked:(id)sender
{
NSString *soapMsg = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<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/\">\
<soap:Body>\
<GetDish xmlns=\"http://www.tempuri.org/\">\
<dishId>%@</dishId>\
</GetDish>\
</soap:Body>\
</soap:Envelope>",
dishNumber.text
];
//---print it to the Debugger Console for verification---
NSLog(soapMsg);
NSURL *url = [NSURL URLWithString: @"http://urllocalhost/mywebservice.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//---set the headers---
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://www.tempuri.org/GetDish" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn)
{
webData = [NSMutableData data];
}
}
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response
{
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
[webData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(theXML);
[activityIndicator stopAnimating];
}
所以在运行这段代码之后,一切看起来都很好。但这里的问题是系统只能从我的 XML 文件中返回整数属性,例如 Menu_Id、dish_Price、dish_category,其值都为 0。
这是点击按钮后通过NSLog获取的消息:
2013-12-30 11:24:00.608 ws[6468:70b] soap message :<?xml version="1.0" encoding="utf-8"?><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/"><soap:Body><GetDish xmlns="http://www.tempuri.org/"><dishId>3</dishId></GetDish></soap:Body></soap:Envelope>
2013-12-30 11:24:01.489 ws[6468:70b] DONE. Received Bytes: 420
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetDishResponse xmlns="http://tempuri.org/"><GetDishResult><Menu_Id>0</Menu_Id><Dish_Price>0</Dish_Price><Dish_Categorie>0</Dish_Categorie></GetDishResult></GetDishResponse></soap:Body></soap:Envelope>
所以任何人都可以帮助解决这个问题。提前致谢。
【问题讨论】:
-
把你实际执行的代码。未注释代码。 NSLog(theXML);
-
您好,感谢您的回复,我已经用 NSLog 消息编辑了问题
-
您得到的响应不是错误而是有效的 XML。所以看起来你在服务器端搞砸了一些事情。首先在浏览器中检查您的 SOAP 服务。它在浏览器中是否正常工作?
-
当我在浏览器中启动我的服务时("41.142.251.142/Food_Delivery/MyWS.asmx?op=GetDish") 系统显示 Soap 请求和响应但我看不到 xml 文件结果,您可以测试 URL。但是有一条消息说说:测试表单仅适用于来自本地机器的请求。
-
我认为网络服务有问题。在这里查看wsdlbrowser.com/…
标签: ios xcode web-services