Mysql 将NSURLConnection替换为NSURLSession以下载和解析json提要
我是在iOS上使用数据库领域的初学者。然而,我可以找到一种连接MySQL数据库、下载和解析json提要的方法。现在在iOS 9中,我不能再使用NSURLConnection了,这就是为什么我必须用NSURLSession替换它。例如,我在这里看到了许多教程。到目前为止,我还无法更换它。因为时间紧迫,我不能再浪费时间了。有人能帮我更换吗 我的代码看起来就像这样:Mysql 将NSURLConnection替换为NSURLSession以下载和解析json提要,mysql,objective-c,json,database,parsing,Mysql,Objective C,Json,Database,Parsing,我是在iOS上使用数据库领域的初学者。然而,我可以找到一种连接MySQL数据库、下载和解析json提要的方法。现在在iOS 9中,我不能再使用NSURLConnection了,这就是为什么我必须用NSURLSession替换它。例如,我在这里看到了许多教程。到目前为止,我还无法更换它。因为时间紧迫,我不能再浪费时间了。有人能帮我更换吗 我的代码看起来就像这样: - (void)downloadItems { // Download the json file NSURL *jso
- (void)downloadItems
{
// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://myhost.ch/test.php"];
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
#pragma mark NSURLConnectionDataProtocol Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// Initialize the data object
_downloadedData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the newly downloaded data
[_downloadedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the locations
NSMutableArray *_locations = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
// Create a new location object and set its props to JsonElement properties
Location *newLocation = [[Location alloc] init];
newLocation.idS = jsonElement[@"idStatistic"];
newLocation.temp = jsonElement[@"temp"];
newLocation.hum = jsonElement[@"hum"];
newLocation.date_time = jsonElement[@"date_time"];
// Add this question to the locations array
[_locations addObject:newLocation];
}
// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
[self.delegate itemsDownloaded:_locations];
}
}
-(无效)下载项目
{
//下载json文件
NSURL*jsonFileUrl=[NSURL URLWithString:@”http://myhost.ch/test.php"];
//创建请求
NSURLRequest*urlRequest=[[NSURLRequest alloc]initWithURL:jsonFileUrl];
//创建NSURLConnection
[nsurlConnectionConnectionWithRequest:urlRequest委托:self];
}
#pragma标记NSURLConnectionDataProtocol方法
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
//初始化数据对象
_下载数据=[[NSMutableData alloc]init];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
//附加新下载的数据
[_下载数据appendData:data];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
//创建一个数组来存储位置
NSMUTABLEARRY*_位置=[[NSMUTABLEARRY alloc]init];
//解析传入的JSON
n错误*错误;
NSArray*jsonArray=[NSJSONSerialization JSONObjectWithData:\u下载数据选项:NSJSONReadingAllowFragments错误:&错误];
//循环浏览Json对象,创建问题对象并将它们添加到我们的问题数组中
for(int i=0;i
您可以试试这个
{
NSURL*url=[NSURL URLWithString:@''
}
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:theRequest
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
responseDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Result:%@", responseDict);
}];
[task resume];