ios同步、异步请求

以下是运用原生态网络请求,ASIHTTP框架对它进行了封装,用法大同小异。

1、同步请求

- (void) initRequestData : (RequestVO *) requestParam{
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *directory = [[NSMutableDictionary alloc]init];
    NSString *paramJson;
    NSMutableString *urlStr = [[NSMutableString alloc] initWithString:REQUEST_URL];
    [directory setValue:requestParam.className forKey:@"className"];
    [directory setValue:requestParam.methodName forKey:@"methodName"];
    [paramDic setValue:requestParam.specialCode forKey:@"specialCode"];
    [paramDic setValue:requestParam.start forKey:@"start"];
    [paramDic setValue:requestParam.end forKey:@"end"];
    [directory setValue:paramDic forKey:@"parameter"];
    if ([NSJSONSerialization isValidJSONObject:directory]) {
        NSError *error ;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:directory options:NSJSONWritingPrettyPrinted error:&error];
        paramJson =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        paramJson = [paramJson stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    [urlStr appendString:paramJson];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    NSHTTPURLResponse *response;
    NSError *error ;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    
    NSDictionary *dataDictory = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"dataDictory : %@",[dataDictory description]);
    }
}

 2、异步请求

@interface CatalogViewController : UIViewController<NSURLConnectionDataDelegate>

@property (strong,nonatomic) NSMutableData *receivedData;
@end


//
//  CatalogViewController.m
//  IpadLisShow
//
//  Created by Dwen on 13-1-21.
//  Copyright (c) 2013年 Dwen. All rights reserved.
//

#import "CatalogViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface CatalogViewController ()

@end

@implementation CatalogViewController
@synthesize receivedData;

//初始化请求数据
- (void) initRequestData : (RequestVO *) requestParam{
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *directory = [[NSMutableDictionary alloc]init];
    NSString *paramJson;
    NSMutableString *urlStr = [[NSMutableString alloc] initWithString:REQUEST_URL];
    [directory setValue:requestParam.className forKey:@"className"];
    [directory setValue:requestParam.methodName forKey:@"methodName"];
    [paramDic setValue:requestParam.specialCode forKey:@"specialCode"];
    [paramDic setValue:requestParam.orderPa forKey:@"orderPa"];
    [paramDic setValue:requestParam.sort forKey:@"sort"];
    [paramDic setValue:requestParam.start forKey:@"start"];
    [paramDic setValue:requestParam.end forKey:@"end"];
    [directory setValue:paramDic forKey:@"parameter"];
    NSLog(@"[directory description] :%@",[directory description]);
    if ([NSJSONSerialization isValidJSONObject:directory]) {
        NSError *error ;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:directory options:NSJSONWritingPrettyPrinted error:&error];
        paramJson =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        paramJson = [paramJson stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    [urlStr appendString:paramJson];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection) {
        receivedData = [[NSMutableData alloc] init];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器连接异常" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

#pragma connection
//接收响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [receivedData setLength:0];
}

//接收到数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [receivedData appendData:data];
}

//数据加载完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSError *error ;
    NSDictionary *dataDictory = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];
    //    NSLog(@"dataDictory : %@",[dataDictory description]);
    if (NULL != dataDictory) {
        //拍品信息
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该拍品无数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"Error : %@",error);
}

@end

 

 

你可能感兴趣的:(异步请求)