【问题标题】:How to call webService Using NSURLConnection in ios如何在ios中使用NSURLConnection调用webService
【发布时间】:2016-06-17 12:07:11
【问题描述】:

实际上,我现在正在使用 JSON 类来调用 Web 服务,但现在我想使用 NSURLConnection 调用该 Web 服务,任何人都可以为我提供代码。

请提供我必须导入的框架的详细信息。

提前谢谢你。

【问题讨论】:

  • "NSURLConnection + JSON" 没有给你回复? Google 上的第一个链接之一:stackoverflow.com/questions/20995815/…
  • 您可以使用 AFNetworking 库进行 Web 服务调用。
  • 你为什么使用NSURLConnection?它在 iOS 9.0 中已弃用。而是使用 NSURLSession 相同。您可以参考 Mattt Thompson 的 tutorial on NSURLSession

标签: ios objective-c nsurlconnection sendasynchronousrequest


【解决方案1】:
NSURL *url = [NSURL URLWithString:stringurl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
     NSLog(@"%@",dictionary);
 }];

你可以用这个。

【讨论】:

  • 但强烈建议不要使用新代码。首选[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {...}];
【解决方案2】:

你可以使用同步来做这样的事情:

NSURL *url=[NSURL URLWithString:urlString];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSString *response=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dd=[response JSONValue];

或使用委托方法

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURLResponse *response = nil;


 // Create url connection and fire request
       NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
      [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];


#pragma mark NSURLConnection Delegate Methods

- (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
    NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
                                                         options:kNilOptions
                                                           error:&error];

   // NSArray* latestLoans = [json objectForKey:@"loans"];

    NSLog(@"json: %@", json);


    [_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse*)cachedResponse {
    // Return nil to indicate not necessary to store a cached response for this connection
    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

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // The request has failed for some reason!
    // Check the error var
}

【讨论】:

    【解决方案3】:

    使用以下代码调用 SOAP Web 服务 (POST):

    -(NSString *)posturl:(NSString *)url withpoststring:(NSString *)postString {

       NSString *post = postString;
       NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
       NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
       NSString *URL = url;
       NSLog(@"%@", URL);
       NSLog(@"%@",post);
    
       [request  setURL:[NSURL URLWithString:URL]];
       [request setHTTPMethod:@"POST"];
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
       [request setHTTPBody:postData];
    
       NSError *error;
       NSURLResponse *response;
       NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
       NSString *data = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    
       if ([data isEqualToString:@""]) {
    
       } else {
          data = stringByStrippingHTML(data);
       }
    
       return data;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-02
      • 2012-02-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多