Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 将NSURLConnection替换为NSURLSession以下载和解析json提要_Mysql_Objective C_Json_Database_Parsing - Fatal编程技术网

Mysql 将NSURLConnection替换为NSURLSession以下载和解析json提要

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

我是在iOS上使用数据库领域的初学者。然而,我可以找到一种连接MySQL数据库、下载和解析json提要的方法。现在在iOS 9中,我不能再使用NSURLConnection了,这就是为什么我必须用NSURLSession替换它。例如,我在这里看到了许多教程。到目前为止,我还无法更换它。因为时间紧迫,我不能再浪费时间了。有人能帮我更换吗

我的代码看起来就像这样:

- (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];