自己实现异步发送请求和图片

简介: 自己实现异步发送请求和图片

异步发送比同步发送请求有不阻塞主线程和支持多线程发送请求的好处。

异步发送请求可以抽象为标准的代理对象,具有使用方便,可扩充性强,只要你足够牛可以写出比第三方控件更牛的发送模块,由于是你自己写的,可以根据自己的意愿对请求的各种细节进行处理。这个异步发送请求处理模块也是对苹果代理具体实现的经典案例。

chttpsendAsynchronousRequest.h

#import <Foundation/Foundation.h>

@protocol chttpsendAsynchronousDelegate <NSObject>

@required
- (void)requestDidFinish:(NSString *)responseData responseType:(NSInteger)type;

@end


@interface chttpsendAsynchronousRequest : NSObject
@property (strong, nonatomic) id <chttpsendAsynchronousDelegate> pSelfView;
- (void)sendHttpRequest:(NSString*)sendString;
- (void)sendImageDataToServer:(UIImage *)image;
@end

chttpsendAsynchronousRequest.m

#import "chttpsendAsynchronousRequest.h"
#import "config.h"

@implementation chttpsendAsynchronousRequest
@synthesize pSelfView;
- (void)sendHttpRequest:(NSString*)sendString
{

    //这个是服务器的地址,暂时随便写地址,不然就把用户的服务器信息泄密了
    NSURL *serverNUSUrl = [NSURL URLWithString:@"http://120.139.148.144:8090/test/main.do"];


    NSLog(@"sendString %@",sendString);

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverNUSUrl
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

    [request setHTTPMethod:@"POST"];

    [request setValue:@"text.html" forHTTPHeaderField:@"Content-Type"];

    NSString * sendData = sendString;
    NSData *postData = [sendData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];
    [NSURLConnection
     sendAsynchronousRequest:request
     queue:[[NSOperationQueue alloc] init]
     completionHandler:^(NSURLResponse *response,
                         NSData *data,
                         NSError *error)
     {
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
         if ([data length] >0 && error == nil && [httpResponse statusCode] == 200)
         {
             NSString * recvStrData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
             if(nil != recvStrData)
             {
                 NSLog(@"recvStrData[%@]",recvStrData);

             }
             if (pSelfView && [pSelfView respondsToSelector:@selector(requestDidFinish:responseType:)]) {
                 [pSelfView requestDidFinish:recvStrData responseType:0];
             }

         }
         else if ([data length]  == 0 && error == nil && [httpResponse statusCode] == 200)
         {

             if (pSelfView && [pSelfView respondsToSelector:@selector(requestDidFinish:responseType:)]){
                 [pSelfView requestDidFinish:nil responseType:1];
             }

         }
         else
         {
             if (pSelfView && [pSelfView respondsToSelector:@selector(requestDidFinish:responseType:)]){
                 [pSelfView requestDidFinish:nil responseType:-1];
             }

         }
     }];

    return;
}

- (void)sendImageDataToServer:(UIImage *)image {
    //分界线的标识符
    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
    NSString *urlString =  @"http://192.168.2.77:8080/GUT/IOSServer";
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:10];
    NSData *image2 = UIImagePNGRepresentation(image);
    NSMutableData *myRequestData=[NSMutableData data];
    [myRequestData appendData:image2];
    NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
    [request setValue:content forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:myRequestData];
    [request setHTTPMethod:@"POST"];
    //建立连接,设置代理
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(conn){
        NSLog(@"连接成功");
    }

    NSHTTPURLResponse *urlResponese = nil;
    NSError *error = [[NSError alloc]init];
    NSData* resultData = [NSURLConnection sendSynchronousRequest:request   returningResponse:&urlResponese error:&error];
    NSString* result= [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
    if([urlResponese statusCode] >=200&&[urlResponese statusCode]<300){
        NSLog(@"返回结果=====%@",result);
    }


    //提示用户上传成功  需要判断返回的json
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"通知"
                                                   message:@"图片上传成功"
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                         otherButtonTitles:nil, nil];
    [alert show];
    NSLog(@"%@",returnString);

}

@end

具体的应用。

发送请求部分:

- (void) searchNotificationMethodRequest
{
    @try {

    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];

    //如果设置此属性则当前的view置于后台
    HUD.dimBackground = NO;

    //设置对话框文字
    HUD.labelText = @"请稍等...";

    //显示对话框
    [HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        [self myProgressTask];
        //sleep(1);
    } completionBlock:^{
        //操作执行完后取消对话框
        [HUD removeFromSuperview];
        HUD = nil;
    }];

    chttpsendAsynchronousRequest *phttpsendAsynchronousRequest = [[chttpsendAsynchronousRequest alloc] init ];
    NSString *messageString = nil;
    NSString *sendString = nil;
    //3. 构造数据
    CHeadEncode *pHeadEncode = [[CHeadEncode alloc] init ];
    messageString = [pHeadEncode headEncode : strToken : @"0x000C" : @"" : @"" : nil];
    if((nil == messageString) || (nil == strVId))
    {
        taskFlag = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新UI操作
            //.....
            [self hudWasHidden:HUD];
        });
        return;

    }
    sendString = [NSString stringWithFormat:@"%@,\"body\":{\"v_id\":\"%@\"}}", messageString, strVId];
    LogInfo(@"sendString = %@", sendString);
    //4. 发送数据
    phttpsendAsynchronousRequest.pSelfView = self;
    [phttpsendAsynchronousRequest sendHttpRequest:sendString];
    sendString = nil;

    }
    @catch (NSException *exception) {
        LogError(@"NSException : %@", exception);
    }
    @finally {

    }

}

请求响应代理函数实现:

- (void)requestDidFinish:(NSString *)responseData responseType:(NSInteger)type {
    if(nil == responseData)
    {
        LogInfo(@"responseData = nil; Type:%d", type);
    }
    else
    {

        LogInfo(@"responseData: %@  Type:%d",responseData,type);
    }


    if (type == 0) {
        if(!_setState)
        {
            [self analyzeMessage:responseData];
        }
        else
        {
            [self analyzeSettingMessage:responseData];
        }

    } else {
        [self failHandle:type];
    }
}
- (void)failHandle:(NSInteger) resultCode
{

    taskFlag = YES;
    dispatch_async(dispatch_get_main_queue(), ^{
        //更新UI操作
        //.....
        [self hudWasHidden:HUD];
    });
    if(1 == resultCode)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新UI操作
            //.....
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"服务器异常!" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
            [alert show];
            [theLock unlock];
            //[self hudWasHidden:HUD];

            //[self switchTaskWayBillSearchViewController];
            //return;
        });

    }
    else
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新UI操作
            //.....
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"网络异常!" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
            [alert show];
            [theLock unlock];
            //[self hudWasHidden:HUD];

            //[self switchTaskWayBillSearchViewController];
            //return;
        });

    }


}

具体的对响应页面的处理部分:

- (void)analyzeMessage:(NSString *)recvData
{

    LogInfo(@"recvData[%@]",recvData);

    dispatch_async(dispatch_get_main_queue(), ^{
        //更新UI操作
        taskFlag = YES;
        [self hudWasHidden:HUD];
    });

    NSMutableDictionary *rootDic = [recvData JSONValue];


    NSDictionary *headDic = [rootDic objectForKey:@"head"];
    if(![self DecodeHeader:headDic])
    {
        [theLock unlock];
        return;
    }


    NSDictionary *bodDictionary = [rootDic objectForKey:@"body"];
    self.next_page = [bodDictionary objectForKey:@"next_page"];
    LogInfo(@"next_page : %@",self.next_page);


    NSMutableArray *bodyArr = [bodDictionary objectForKey:@"data_list"];
    NSDictionary *bodyDic = nil;
    NSUInteger  i = 0;
    for (i = 0; i < bodyArr.count; i++)
    {
        bodyDic = [bodyArr objectAtIndex:i];

        CWaringInfo *waringinfo = [[CWaringInfo alloc] init];
        NSString *feildStr = [bodyDic objectForKey:@"alarm_id"];
        if(nil == feildStr)
        {
            [theLock unlock];
            return;
        }
        else
        {

            [waringinfo setAlarm_id:feildStr];
        }
        LogInfo(@"waringinfo = [%@]", feildStr);
        feildStr = [bodyDic objectForKey:@"alarm_name"];
        if(nil == feildStr)
        {
            [theLock unlock];
            return;
        }
        else
        {
            [waringinfo setAlarm_name:feildStr];
        }
        LogInfo(@"waringinfo = [%@]", feildStr);
        feildStr = [bodyDic objectForKey:@"alarm_date"];
        if(nil == feildStr)
        {
            [theLock unlock];
            return;
        }
        else
        {
            [waringinfo setAlarm_date:feildStr];
        }
        LogInfo(@"waringinfo = [%@]", feildStr);
        feildStr = [bodyDic objectForKey:@"alarm_desc"];
        if(nil == feildStr)
        {
            [theLock unlock];
            return;
        }
        else
        {
            [waringinfo setAlarm_desc:feildStr];

        }
        LogInfo(@"waringinfo = [%@]", feildStr);

        [self.computers addObject:waringinfo];


    }



    LogInfo(@"OldTaskArray = [%@]", self.computers);
    dispatch_async(dispatch_get_main_queue(), ^{
        //更新UI操作
        //.....
        [self.tableView reloadData];
        [theLock unlock];
    });


}
目录
相关文章
|
XML 测试技术 uml
liteflow学习一
liteflow学习一
1824 0
|
消息中间件 缓存 监控
在FaaS中,如何设计无状态的函数来确保数据处理的一致性?
在FaaS中,如何设计无状态的函数来确保数据处理的一致性?
|
XML JSON JavaScript
从解决跨域CSOR衍生知识 Network 网络请求深度解析:从快递系统到请求王国-优雅草卓伊凡
从解决跨域CSOR衍生知识 Network 网络请求深度解析:从快递系统到请求王国-优雅草卓伊凡
355 0
从解决跨域CSOR衍生知识 Network 网络请求深度解析:从快递系统到请求王国-优雅草卓伊凡
|
SQL DataWorks 安全
DataWorks产品使用合集之如何实现分钟级调度
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
物联网 消息中间件 调度
从零到百万级iot系统架构演进(会不断更新
2年从单应用架构,到现在基础中台的iot架构,从零到现在注册用户百万
7670 0
从零到百万级iot系统架构演进(会不断更新
|
存储 运维 监控
什么是多云? 为什么我们需要多云可观测性 (Observability)?
NineData的数据库工程师对多云的思考:多云是今天的现实,也是未来的趋势 (公共云基础设施用户中有86%正在实施多云策略); • 多云环境下的数据管理成为新的需求,包括文章中提到的Observability; • 云环境越来越复杂,既是困难,也是机遇。
639 0
|
资源调度 小程序 JavaScript
微信小程序如何使用vant组件?
微信小程序如何使用vant组件?
549 0
|
SQL 存储 关系型数据库
PolarDB-X 1.0-用户指南—SQL调优指南—SQL调优基础概念
在使用PolarDB-X的过程中,可能出现性能不符合预期的慢SQL。SQL调优的过程,就是通过分析SQL的执行计划、各阶段运行时长等信息,找出导致SQL执行慢的原因,继而解决问题。
444 0
PolarDB-X 1.0-用户指南—SQL调优指南—SQL调优基础概念
|
人工智能 大数据 5G
2019年美国对华风投不到40亿美元,为六年最年水平|全球快讯
研究公司Rhodium Group(总部位于纽约)和美中关系全国委员会周一联合发布的一份报告显示,随着中美科技纠纷加深,2019年美国对华风险投资预计跌至六年来的最低点。