【问题标题】:unable to load data from webservice into id type of variable无法将 web 服务中的数据加载到 id 类型的变量中
【发布时间】:2015-08-21 05:12:54
【问题描述】:

我正在尝试将数据从服务器加载到 id 结果变量中,我的 url 工作正常,我可以在浏览器上看到数据,但是数据加载过程非常慢(15 秒),并且得到输出数据 id 结果的结果是无

类:MyWebservices:-

-(id)getResponseFromServer:(NSString*)requestString
{
  id result;
  NSError *error;
NSURLResponse *response = nil;
NSURL *url = [NSURL URLWithString:[requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    NSData * resultData= [[NSData alloc]init];
    resultData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 

类:WebserviceCallingClass

 - (void)viewDidLoad
  {

id result =  [AppDelegate.MyWebservices  getResponseFromServer:urlString] ;

}

【问题讨论】:

    标签: objective-c json nsurlconnection nsurlconnectiondelegate


    【解决方案1】:

    使用异步请求。

    1) 使用 NSURLConnectionDelegate 并声明 在你的接口类a中:

    NSMutableData *_responseData;
    

    2)发送您的异步请求,并在超时间隔中设置大于 15 秒的时间

     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://uri"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
            conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
            [conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
            [conn start];
    

    3) 实现您的委托方法

        - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
            // A response has been received, this is where we initialize the instance var you created
            // so that we can append data to it in the didReceiveData method
            // Furthermore, this method is called each time there is a redirect so reinitializing it
            // also serves to clear it
    
            _responseData = [[NSMutableData alloc] init];
        }
    
        - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
            // Append the new data to the instance variable you declared
            [_responseData appendData:data];
        }
    
        - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                          willCacheResponse:(NSCachedURLResponse*)cachedResponse {
            // Return nil to indicate not necessary to store a cached response for this connection
            //NSLog(@"cache");
            return nil;
        }
    
        - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
            // The request is complete and data has been received
            // You can parse the stuff in your instance variable now
    
    
        }
    
    }
    

    或在同步请求中尝试编辑您的 NSMutableURLRequest

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.f];
    

    【讨论】:

    • 感谢回复,get id result nil 是什么原因,同步请求是不可能的吗?,谢谢
    • 什么 NSLog(@"Error getting %@, HTTP status code %li", url, (long)[response statusCode]);打印?
    • 什么是 NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returnedResponse:&response error:&error]; NSLog(@"%@",error);打印?
    • 错误域=NSURLErrorDomain Code=-1001 "请求超时。" UserInfo=0x7f9739383de0 {NSUnderlyingError=0x7f973937d050“请求超时。”
    • 你在用模拟器吗?您是否检查过模拟器是否已连接到互联网? (尝试 Safari)
    猜你喜欢
    • 2018-06-23
    • 2019-09-01
    • 2020-09-17
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    相关资源
    最近更新 更多