大家如何获取数组中的总响应的代码实际上我已经实现了代码,如下所示我的问题是当我在下一个类中调用方法时我得到数组响应null所以plz帮助我在iphone中避免这个问题。
我的代码是:
- (无效)countrySelection {
NSString *jobSearchUrlString = [NSString stringWithFormat:@"http://infra2appsmobile.cloudapp.net/Infra2Apps.svc/Countries"];
//NSLog(@"url for new articles is = %@",jobSearchUrlString);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:jobSearchUrlString]];
NSURLConnection *theconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theconnection) {
RoutData = [[NSMutableData alloc] init];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[RoutData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[RoutData appendData:data];
NSString *thexml = [[NSString alloc] initWithData:RoutData encoding:NSUTF8StringEncoding];
NSDictionary *dictXML= [XMLReader dictionaryForXMLString:thexml error:nil];
NSMutableArray *arrStation = [[dictXML objectForKey:@"feed"] objectForKey:@"entry"] ;//this would return the array of station dictionaries
for (int i=0; i<[arrStation count]; i++) {
NSLog(@"---->> String Type Country names <<---: %@",[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:CountryName"] objectForKey:@"text"]);
[materialarray addObject:[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:CountryName"] objectForKey:@"text"]];
NSLog(@"--->>** Array Type Country names **<<<---%@",materialarray);
}
}
我的回答是:
2012-03-09 12:49:40.856 TableSample [4862:f803] ----&gt;&gt;字符串类型国家/地区名称&lt;&lt; ---: 法国 2012-03-09 12:49:40.867 TableSample [4862:f803] ---&gt;&gt; **数组类型国家/地区名称&lt;&lt;&lt; ---(null) 2012-03-09 12:49:40.868 TableSample [4862:f803] ----&gt;&gt;字符串类型国家/地区名称&lt;&lt; ---: MEA HQ 2012-03-09 12:49:40.869 TableSample [4862:f803] ---&gt;&gt; 数组类型国家/地区名称&lt;&lt;&lt; ---(null) 2012-03-09 12:49:40.870 TableSample [4862:f803] ----&gt;&gt;字符串类型国家/地区名称&lt;&lt; ---: 以色列 2012-03-09 12:49:40.871 TableSample [4862:f803] ---&gt;&gt; 数组类型国家/地区名称&lt;&lt;&lt; ---(null) 2012-03-09 12:49:40.872 TableSample [4862:f803] ----&gt;&gt;字符串类型国家/地区名称&lt;&lt; ---: 火鸡 2012-03-09 12:49:40.879 TableSample [4862:f803] ---&gt;&gt; 数组类型国家/地区名称&lt;&lt;&lt; ---(null) 2012-03-09 12:49:40.879 TableSample [4862:f803] ----&gt;&gt;字符串类型国家/地区名称&lt;&lt; ---: 阿联酋 2012-03-09 12:49:40.880 TableSample [4862:f803] ---&gt;&gt; 数组类型国家/地区名称&lt;&lt;&lt; ---(null) 2012-03-09 12:49:40.881 TableSample [4862:f803] ----&gt;&gt;字符串类型国家/地区名称&lt;&lt; ---: SA 2012-03-09 12:49:40.881 TableSample [4862:f803] ---&gt;&gt; 数组类型国家/地区名称&lt;&lt;&lt; ---(null) 2012-03-09 12:49:40.882 TableSample [4862:f803] ----&gt;&gt;字符串类型国家/地区名称&lt;&lt; ---: RSA 2012-03-09 12:49:41.020 TableSample [4862:f803] ---&gt;&gt; 数组类型国家/地区名称&lt;&lt;&lt; ---(null) 2012-03-09 12:49:41.021 TableSample [4862:f803] ----&gt;&gt;字符串类型国家/地区名称&lt;&lt; ---: 我们总部 2012-03-09 12:49:41.022 TableSample [4862:f803] ---&gt;&gt; 数组类型国家/地区名称**&lt;&lt;&lt; ---(null
答案 0 :(得分:2)
在connectionDidFinishLoading
委托方法中,您将获得响应字符串,您需要在该响应字符串上调用JSONValue
,它将以数组形式提供结果。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseString %@",responseString);
self.responseData = nil;
NSArray *responseArray = [responseString JSONValue];
[responseString release];
}
答案 1 :(得分:1)
请确保已分配materialarray
。
我认为存在问题所在。
希望这有帮助。