// 网络服务请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
//returndata is response of webservice
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSDictionary *results = [responseString JSONValue] ;
NSLog(@"chat data- %@",results);
NSString *strResults = [results objectForKey:@"d"];
NSLog(@"result string is-%@",strResults);
这是我用于从 Web 服务获取数据的代码。但是每次访问此页面时,我都必须这样做。这是任何可以将我的数据存储在缓存内存中的方法,所以我不需要每次都请求。
我在用
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
但我不知道如何使用 - (NSCachedURLResponse *) 连接这个方法谢谢...