1

Currentyl 我正在使用 XCode4.2.1 开发 IOS 应用程序。我的应用程序正在从 asp.net Web 服务中获取 json 值。这是我获取 Web 服务的代码。

-(BOOL)GrabbingStudentsWebService{
NSURL *url=[NSURL URLWithString:@"http://www.cheaptal.com/easypft/Service1.asmx"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
[request addRequestHeader:@"SOAPAction" value:@"http://cheaptal.com/GetAllStudents"];

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>"
                    "<GetAllStudents xmlns=\"http://cheaptal.com/\" />"
                    "</soap:Body>"
                    "</soap:Envelope>"];


NSString *messageLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
[request addRequestHeader:@"Content-Length" value:messageLength];
[request appendPostData:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[request setDelegate:self]; 
[request startAsynchronous]; 

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Sync...";

return TRUE;}

这是我的请求完成代码

- (void)requestFinished:(ASIHTTPRequest *)request{    
[MBProgressHUD hideHUDForView:self.view animated:YES];
if (request.responseStatusCode == 200) {

    NSString *response = [request responseString];
    txtview.text = response;
    NSDictionary *insidejson=[[NSDictionary alloc] init];
    insidejson = [response JSONValue]; //my problem is here.
} else {
    txtview.text = @"cannot retrieve data.";
}}

这是来自网络服务的响应代码:

<?xml version="1.0" encoding="utf-8"?><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><GetAllStudentsResponse xmlns="http://cheaptal.com/"><GetAllStudentsResult>[{"stu_name":"COEMAW","stu_sitreach":10.5,"stu_broadjump":12.3,"stu_napfarun":"11:54.32"},{"stu_name":"LINA","stu_sitreach":2.5,"stu_broadjump":5.3,"stu_napfarun":"10:54.22"},{"stu_name":"ULD27","stu_sitreach":6.5,"stu_broadjump":7.3,"stu_napfarun":"09:54.12"},{"stu_name":"MOTTA","stu_sitreach":7.5,"stu_broadjump":9.3,"stu_napfarun":"10:33.52"}]</GetAllStudentsResult></GetAllStudentsResponse></soap:Body></soap:Envelope>

我的问题是,当我尝试使用JSONValue ( insidejson = [response JSONValue]; ) 解析对NSDictionary的响应字符串时, insidejson 变量中没有值。我仍然不知道为什么insidejson是空的。

非常感谢您的帮助!飞镖蛙

4

0 回答 0