iOS进阶

iOS 基础网络篇

2016-08-05  本文已影响17人  Jack__yang

多线程

网络

HTTP请求

// URL
NSString *urlStr = @"http://xxxxx.com";
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:urlStr];

// 请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// URL
NSString *urlStr = @"http://xxxxx.com";
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:urlStr];

// 请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];

数据解析

NSURLConnection(iOS9已经过期)

NSURLSession

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[task resume];
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

}];

[task resume];
// 创建session
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];

// 设置请求头(告诉服务器第1024个字节开始下载)
[request setValue:@"bytes=1024-" forHTTPHeaderField:@"Range"];

// 创建任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request];

// 开始任务
[task resume];
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
    // 打开NSOutputStream

    // 获得文件的总长度

    // 存储文件的总长度

    // 回调
    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    // 利用NSOutputStream写入数据

    // 计算下载进度
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    // 关闭并且清空NSOutputStream

    // 清空任务task
}
NSURLSessionUploadTask *task = [[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[task resume];

AFNetworking(网络请求的三方框架)

// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

NSDictionary *params = @{
                         @"username" : @"520it",
                         @"pwd" : @"520it"
                         };

[mgr GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"请求失败---%@", error);
}];
// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

NSDictionary *params = @{
                         @"username" : @"520it",
                         @"pwd" : @"520it"
                         };

[mgr POST:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"请求失败---%@", error);
}];
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

[mgr POST:@"http://120.25.226.186:32812/upload" parameters:@{@"username" : @"123"}
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    // 在这个block中设置需要上传的文件
//            NSData *data = [NSData dataWithContentsOfFile:@"/Users/xiaomage/Desktop/placeholder.png"];
//            [formData appendPartWithFileData:data name:@"file" fileName:@"test.png" mimeType:@"image/png"];

//            [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/placeholder.png"] name:@"file" fileName:@"xxx.png" mimeType:@"image/png" error:nil];

        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/placeholder.png"] name:@"file" error:nil];
} success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"-------%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {

}];

UIWebView

上一篇 下一篇

猜你喜欢

热点阅读