使用dataTaskWithRequest检索JSON数据:无法正常工作

时间:2016-10-07 12:35:04

标签: ios objective-c json nsdata nsurlsessiondatatask

我使用以下代码从服务器获取数据。使用以下代码时,它工作正常。

NSData *data = [NSData dataWithContentsOfURL:URL options:NSDataReadingUncached error:&error];

if(error==nil){

    NSDictionary *json = [NSJSONSerialization
                          JSONObjectWithData:data
                          options:NSJSONReadingMutableContainers
                          error:&error];
}

成功将数据转换为json格式。

但是当我尝试使用以下格式获取数据时,

NSDictionary *proxyDict = @{
                            @"HTTPEnable"  : [NSNumber numberWithInt:1],
                            (NSString *)kCFStreamPropertyHTTPProxyHost  : proxyHost,
                            (NSString *)kCFStreamPropertyHTTPProxyPort  : proxyPort,
                            (NSString *)kCFProxyUsernameKey : hostUserName,
                            (NSString *)kCFProxyPasswordKey : hostPassword,

                            @"HTTPSEnable" : [NSNumber numberWithInt:1],
                            (NSString *)kCFStreamPropertyHTTPSProxyHost : proxyHost,
                            (NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPort,
                            (NSString *)kCFProxyUsernameKey : hostUserName,
                            (NSString *)kCFProxyPasswordKey : hostPassword,
                            };

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];
sessionConfig.connectionProxyDictionary = proxyDict;

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];
sessionConfig.connectionProxyDictionary = proxyDict;

NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: sessionConfig delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

__block NSDictionary *returnJsonDictionary;
__block NSError *error1;

NSURLSessionDataTask *task = [defaultSession dataTaskWithRequest:URLrequest completionHandler:
                              ^(NSData *data, NSURLResponse *response, NSError *error) {
                                  NSLog(@"NSURLSession got the response [%@]", response);
                                  NSLog(@"NSURLSession got the data [%@]", data);

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error1];

返回

<HTML><HEAD>
<meta http-equiv=pragma content=nocache>
<META HTTP-EQUIV=Expires CONTENT=-1>
<SCRIPT>
    location.href="https://myHostAddress:port/httpclient.html"
</SCRIPT>
</HEAD><BODY>
</BODY>
</HTML>

1 个答案:

答案 0 :(得分:0)

使用它可以正常工作。

 NSURL *myUrl=[NSURL URLWithString:@"https://itunes.apple.com/search?term=jack+johnson&entity=musicVideo"];
        NSURLRequest *myRequest=[NSURLRequest requestWithURL:myUrl];

        NSURLSessionConfiguration *mySessionConfiguration=[NSURLSessionConfiguration defaultSessionConfiguration];

        NSURLSession *mySession=[NSURLSession sessionWithConfiguration:mySessionConfiguration delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

        NSURLSessionDataTask *myDataTask=[mySession dataTaskWithRequest:myRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
        {
            NSDictionary *myDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

             NSLog(@"the value is dictionary is %@",myDict);

        }];

        [myDataTask resume];