随笔 - 27, 文章 - 0, 评论 - 29, 阅读 - 51238
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

2016年7月28日

#import <Foundation/Foundation.h>

#import "AFNetworking.h"

 

@interface AFHttpClient : NSObject

 

DEFINE_SINGLETON_FOR_HEADER(AFHttpClient);

 

/**

 *  判断网络是否正常

 *

 *  @return Bool YES 表示正常

 */

- (BOOL)isOnline;

/**

 *  停止请求

 */

- (void)cancelAllRequest;

/**

 *  Get 请求

 *

 *  @param path       路径

 *  @param parameters 参数

 *  @param success    success block

 *  @param failure    failure block

 */

- (AFHTTPRequestOperation *)getHttpRequest:(NSString *)path

             parameter:(NSDictionary *)parameters

               success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

/**

 *  Get 请求

 *

 *  @param url        路径

 *  @param parameter  参数

 *  @param completion completion block

 */

- (AFHTTPRequestOperation *)getHttpRequest:(NSString *)path

              paramter:(NSDictionary *)parameters

            completion:(void (^)(BOOL success))completion;

/**

 *  Post 请求

 *

 *  @param path       路径

 *  @param parameters 参数

 *  @param success    success block

 *  @param failure    failure block

 */

- (AFHTTPRequestOperation*)postHttpRequest:(NSString *)path

              parameter:(NSDictionary *)parameters

                success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

                failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

 

/**

 *  Post 请求 发送json数据到服务端

 *

 *  @param path            路径

 *  @param parameters      参数

 *  @param success         success description

 *  @param postHttpRequest postHttpRequest description

 *  @param path            path description

 *  @param parameters      parameters description

 *  @param completion      completion description

 */

- (AFHTTPRequestOperation*)postJsonHttpRequest:(NSString *)path

                  parameter:(NSDictionary *)parameters

                    success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

                    failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

/**

 *  Post 请求

 *

 *  @param url        路径

 *  @param parameter  参数

 *  @param completion completion block

 */

- (AFHTTPRequestOperation*)postHttpRequest:(NSString *)path

               paramter:(NSDictionary *)parameters

             completion:(void (^)(BOOL success))completion;

/**

 *  上传图片

 *

 *  @param uploadPath 上传路径

 *  @param imagePath  文件路径

 *  @param parameters 参数

 *  @param success    success block

 *  @param failure    failure block

 */

- (AFHTTPRequestOperation *)uploadImage:(NSString *)uploadPath

          imagePath:(NSString *)imagePath

          parameter:(NSDictionary *)parameters

            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

/**

 *  上传图片

 *

 *  @param url        上传路径

 *  @param path       文件本地路径

 *  @param name       文件名

 *  @param parameter  参数

 *  @param completion block

 */

- (AFHTTPRequestOperation *)uploadImage:(NSString *)url

               path:(NSString *)path

               name:(NSString *)name

           paramter:(NSDictionary *)parameter

         completion:(void (^)(BOOL success))completion;

/**

 *  上传图片

 *

 *  @param url       上传路径

 *  @param imageData 图像数据

 *  @param imageName 图像名称

 *  @param fileName  文件名

 *  @param parameter 参数

 *  @param success   success block

 *  @param failure   failure block

 */

- (AFHTTPRequestOperation *)uploadImage:(NSString *)url

          imageData:(NSData *)imageData

          imageName:(NSString *)imageName

           fileName:(NSString*)fileName

           paramter:(NSDictionary *)parameter

            success:(void (^)(AFHTTPRequestOperation *operation,id responseObject))success

            failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))failure;

/**

 *  下载图片保存文件

 *

 *  @param url   地址

 *  @param path  文件名

 *  @param block block

 */

- (AFHTTPRequestOperation *)downloadImage:(NSString*)url

           saveToFile:(NSString *)path

           completion:(void (^)(bool success))block;

@end

 

.m文件

#import "AFHttpClient.h"

#import "AFNetworking.h"

#import "YQZTools.h"

#import "YQZSetting.h"

#import "Reachability.h"

#import "YQZViewController.h"

@interface AFHttpClient()

 

@property (nonatomic, strong) AFHTTPRequestOperationManager *currentRequest;

 

@end

 

@implementation AFHttpClient

 

DEFINE_SINGLETON_FOR_CLASS(AFHttpClient);

 

- (AFHTTPRequestOperationManager *)currentRequest

{

    if (!_currentRequest) {

        self.currentRequest = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:[YQZTools getProxy]]];

    }

    return _currentRequest;

}

 

- (BOOL)isOnline

{

    return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable;

}

 

#pragma mark - 请求 -

 

- (void)cancelAllRequest

{

    [self.currentRequest.operationQueue cancelAllOperations];

}

 

- (AFHTTPRequestOperation *)getHttpRequest:(NSString *)path

             parameter:(NSDictionary *)parameters

               success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

{

    if(NotReachable == [[Reachability reachabilityForInternetConnection] currentReachabilityStatus]){

        if (![[YQZSetting sharedInstance] getAlertViewShow]) {

            [YQZTools alert:@"提示" message:@"您的网络不给力哦"];

            failure(nil,nil);

        }

        return nil;

    }

    

    [self showRequestLogWithUrl:path parameter:parameters];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    

    return [manager GET:path

      parameters:parameters

         success:^(AFHTTPRequestOperation *operation, id responseObject) {

             [self showSuccessLog:responseObject];

             

             int code = [[responseObject objectForKey:@"resultCode"] intValue];

             if (code == 399)      //系统错误

             {

                 failure(operation, nil);

                 return;

             }

             

             if (success)

             {

                 success(operation, responseObject);

             }

    }

         failure:^(AFHTTPRequestOperation *operation, NSError *error) {

             [self showErrorLog:error];

             if (failure) {

                 failure(operation, error);

             }

    }];

}

 

- (AFHTTPRequestOperation *)getHttpRequest:(NSString *)path

              paramter:(NSDictionary *)parameters

            completion:(void (^)(BOOL success))completion

{

    [self showRequestLogWithUrl:path parameter:parameters];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    return [manager GET:path

      parameters:parameters

         success:^(AFHTTPRequestOperation *operation, id responseObject) {

             [self showSuccessLog:responseObject];

             completion(YES);

    }

         failure:^(AFHTTPRequestOperation *operation, NSError *error) {

             [self showErrorLog:error];

             completion(NO);

    }];

}

 

 

- (AFHTTPRequestOperation*)postHttpRequest:(NSString *)path

              parameter:(NSDictionary *)parameters

                success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

                failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

{

    if(NotReachable == [[Reachability reachabilityForInternetConnection] currentReachabilityStatus]){

        if (![[YQZSetting sharedInstance] getAlertViewShow]) {

            [YQZTools alert:@"提示" message:@"您的网络不给力哦"];

            if (failure) {

                failure(nil,nil);

            }

        }

        return nil;

    }

    

    [self showRequestLogWithUrl:path parameter:parameters];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

 

    return [manager POST:path

       parameters:parameters

          success:^(AFHTTPRequestOperation *operation, id responseObject) {

              [self showSuccessLog:responseObject];

              

              int code = [[responseObject objectForKey:@"resultCode"] intValue];

              if (code == 399)      //系统错误

              {

                  failure(operation, nil);

                  return;

              }

              if (success)

              {

                  success(operation, responseObject);

              }

    }

          failure:^(AFHTTPRequestOperation *operation, NSError *error) {

              [self showErrorLog:error];

              failure(operation, error);

              

    }];

}

 

- (AFHTTPRequestOperation*)postJsonHttpRequest:(NSString *)path

                  parameter:(NSDictionary *)parameters

                    success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

                    failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

{

    if(NotReachable == [[Reachability reachabilityForInternetConnection] currentReachabilityStatus]){

        if (![[YQZSetting sharedInstance] getAlertViewShow]) {

            [YQZTools alert:@"提示" message:@"您的网络不给力哦"];

            failure(nil,nil);

        }

        return nil;

    }

    

    [self showRequestLogWithUrl:path parameter:parameters];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    //申明请求的数据是json类型

    manager.requestSerializer=[AFJSONRequestSerializer serializer];

    

    return [manager POST:path

       parameters:parameters

          success:^(AFHTTPRequestOperation *operation, id responseObject) {

              [self showSuccessLog:responseObject];

              

              int code = [[responseObject objectForKey:@"resultCode"] intValue];

              if (code == 399)      //系统错误

              {

                  failure(operation, nil);

                  return;

              }

              if (success)

              {

                  success(operation, responseObject);

              }

          }

          failure:^(AFHTTPRequestOperation *operation, NSError *error) {

              [self showErrorLog:error];

              failure(operation, error);

              

          }];

}

 

- (AFHTTPRequestOperation*)postHttpRequest:(NSString *)path

               paramter:(NSDictionary *)parameters

             completion:(void (^)(BOOL success))completion

{

    [self showRequestLogWithUrl:path parameter:parameters];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    return [manager POST:path

       parameters:parameters

          success:^(AFHTTPRequestOperation *operation, id responseObject) {

              [self showSuccessLog:responseObject];

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

              completion(YES);

    }

          failure:^(AFHTTPRequestOperation *operation, NSError *error) {

              [self showErrorLog:error];

              completion(NO);

    }];

}

 

#pragma mark - 上传下载 -

 

- (AFHTTPRequestOperation *)uploadImage:(NSString *)uploadPath

          imagePath:(NSString *)imagePath

          parameter:(NSDictionary *)parameters

            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSURL *filePath = [NSURL fileURLWithPath:imagePath];

    return [manager POST:uploadPath parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFileURL:filePath name:@"image" error:nil];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"Success: %@", responseObject);

        success(operation, responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@", error);

        failure(operation, error);

    }];

}

 

- (AFHTTPRequestOperation *)uploadImage:(NSString *)url

               path:(NSString *)path

               name:(NSString *)name

           paramter:(NSDictionary *)parameter

         completion:(void (^)(BOOL success))completion

{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    return [manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:name error:nil];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"Upload Success: %@", responseObject);

        completion(YES);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Upload Error: %@", error);

        completion(NO);

    }];

}

 

- (AFHTTPRequestOperation *)uploadImage:(NSString *)url

          imageData:(NSData *)imageData

          imageName:(NSString *)imageName

           fileName:(NSString*)fileName

           paramter:(NSDictionary *)parameter

            success:(void (^)(AFHTTPRequestOperation *operation,id responseObject))success

            failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))failure

{

    

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    return [manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFileData:imageData

                                    name:imageName

                                fileName:fileName

                                mimeType:@"image/*"];

    

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        [self showSuccessLog:responseObject];

        NSLog(@"Upload Success: %@", responseObject);

        success(operation,responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Upload Error: %@", error);

        failure(operation, error);

    }];

}

 

 

- (AFHTTPRequestOperation *)downloadImage:(NSString*)url

           saveToFile:(NSString *)path

           completion:(void (^)(bool success))block

{

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

 

        block(YES);

        NSLog(@"Successfully downloaded file to %@", path);

        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"%@", error);

        block(NO);

    }];

    

    [operation start];

    return operation;

}

 

#pragma mark - 日志 -

 

- (void)showRequestLogWithUrl:(NSString *)path parameter:(NSDictionary *)parameters

{

    NSLog(@"========请求路径==========================");

    NSLog(@"Request URL: \n%@ \nParameter:\n%@", path, parameters);

}

 

- (void)showSuccessLog:(id)responseObject

{

    NSLog(@"========✔️✔️✔️✔️========================");

    NSLog(@"请求成功!返回结果:\n%@", responseObject);

    NSLog(@"=========================================");

}

 

- (void)showErrorLog:(NSError *)error

{

    NSLog(@"========❌❌❌❌========================");

    NSLog(@"请求失败!错误描述:\n%@", error.localizedDescription);

    NSLog(@"=========================================");

}

 

@end

posted @ 2016-07-28 05:53 吕霖 阅读(378) 评论(0) 推荐(0) 编辑

摘要: if (resultCode == 0) { self.imageServerString = [responseObject objectForKey:@"imagesServerURL"]; [self buildOrderListData:responseObject]; [self.cont 阅读全文

posted @ 2016-07-28 05:49 吕霖 阅读(283) 评论(0) 推荐(0) 编辑

摘要: #import <UIKit/UIKit.h> #import "YQZMutableArray.h" @interface YQZViewController : UIViewController @property (nonatomic, strong) NSString *kTitle; // 阅读全文

posted @ 2016-07-28 05:41 吕霖 阅读(447) 评论(0) 推荐(0) 编辑

摘要: #import <UIKit/UIKit.h> #import "SVPullToRefresh.h" #import "YQZViewController.h" @interface YQZPullViewController : YQZViewController { IBOutlet UITa 阅读全文

posted @ 2016-07-28 05:37 吕霖 阅读(231) 评论(0) 推荐(0) 编辑

2015年9月10日

摘要: 编写cell中得button点击事件- (IBAction)showButtonClick:(id)sender{ UIButton *button = (UIButton *)sender; UIWindow* window = [UIApplication sharedApplication... 阅读全文

posted @ 2015-09-10 17:02 吕霖 阅读(782) 评论(0) 推荐(0) 编辑

2014年2月27日

摘要: 举例:首先定义TestModel如下:@interface TestModel : NSObject@property (nonatomic, strong) NSString *name;@property (nonatomic, strong) NSString *desc;@property (nonatomic, assign) int age;@end然后在viewControl中viewDidLoad中添加如下代码 TestModel *model = [[TestModel alloc] init]; model.name = @"Jack"; model.a 阅读全文

posted @ 2014-02-27 18:47 吕霖 阅读(726) 评论(0) 推荐(0) 编辑

摘要: 总是忘记xcode中查找中文,这次记下来,以后就不会忘记了,哈哈请看下图:切换到查找,点击find后面的text,选择Regular Expression,然后输入1. 查找非ascii的字符 @"[^\x00-\xff]+2. 取所有属性变量定义 (\w+)(\s)*\*(?:\s*const )?(\s)*(\w+)3. 取所有属性变量定义, 带_开头的 (\w+)(\s)*\*(?:\s*const )?(\s)*_(\w+)4. 修改私有变量名,可以点击私有变量之后,选择edit all in scope感谢熊总(熊威)的大力帮助! 阅读全文

posted @ 2014-02-27 18:33 吕霖 阅读(700) 评论(0) 推荐(0) 编辑

2013年9月24日

摘要: 之前使用的xcode4.6的插件在升级到xcode5后不能使用了,查了很多资料,终于知道是缺少了DVTPlugInCompatibilityUUIDs请在插件项目plist文件中加入DVTPlugInCompatibilityUUIDsstring值1:63FC1C47-140D-42B0-BB4D-A10B2D225574string值2:37B30044-3B14-46BA-ABAA-F01000C27B63截图:附:Xcode 4 插件制作入门 阅读全文

posted @ 2013-09-24 17:14 吕霖 阅读(1277) 评论(0) 推荐(0) 编辑

2013年2月1日

摘要: 使用前 需引入QuartzCore.framework, 并在相关文件中加入 #import "QuartzCore/QuartzCore.h" 定义 shakeFeedbackOverlay为UIImageView设置 self.shakeFeedbackOverlay.alpha = 0.0; self.shakeFeedbackOverlay.layer.cornerRadius = 10.0; //设置圆角半径1、图像左右抖动 CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@&quo 阅读全文

posted @ 2013-02-01 14:34 吕霖 阅读(2048) 评论(0) 推荐(2) 编辑

2012年1月11日

摘要: 先看截图:核心代码来自:http://www.markj.net/iphone-uiimage-pixel-color/如下所示,原代码有修改:- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { if (self.hidden==YES) { //color wheel is hidden, so don't handle this as a color wheel event. [[self nextResponder] touchesEnded:touches withEvent:event]; ret 阅读全文

posted @ 2012-01-11 00:31 吕霖 阅读(834) 评论(0) 推荐(1) 编辑

点击右上角即可分享
微信分享提示