KHNetworkEngine.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // KHNetworkEngine.m
  3. // KHealth
  4. //
  5. //
  6. #import "KHNetworkEngine.h"
  7. #import "KHNetworkOperation.h"
  8. @implementation KHError
  9. -(NSString *)localizedFailureReason{
  10. if (_reason) {
  11. return _reason;
  12. }
  13. if ([self.domain isEqualToString:NSURLErrorDomain]) {
  14. return @"网络错误";
  15. }
  16. return [self localizedDescription];
  17. }
  18. -(id)initWithMKNetworkOperation:(MKNetworkOperation *)op error:(NSError *)error{
  19. NSInteger statusCode = op.HTTPStatusCode;
  20. id respJson = op.responseJSON;
  21. if ([respJson isKindOfClass:[NSDictionary class]] || [respJson isKindOfClass:[NSArray class]]) {
  22. self = [self initWithDomain:kHAppErrorDomain code:statusCode userInfo:nil];
  23. if ([respJson isKindOfClass:[NSArray class]]) {
  24. NSMutableString *string = [NSMutableString string];
  25. NSInteger idx = 0;
  26. for (NSDictionary *dict in respJson) {
  27. if (idx == 0) {
  28. [string appendString:[dict objectForKey:@"message"]];
  29. }
  30. else{
  31. [string appendFormat:@"\n%@",[dict objectForKey:@"message"]];
  32. }
  33. }
  34. self.reason = string;
  35. }
  36. else{
  37. self.reason = [respJson objectForKey:@"message"];
  38. }
  39. }
  40. else{
  41. self = [self initWithDomain:error.domain code:error.code userInfo:error.userInfo];
  42. self.reason = [error localizedFailureReason];
  43. }
  44. return self;
  45. }
  46. @end
  47. @implementation KHNetworkEngine
  48. -(BOOL) isCacheable{
  49. return NO;
  50. }
  51. -(id)init
  52. {
  53. self = [self initWithHostName:KHServerBase];
  54. if (self) {
  55. // self.portNumber = 8100;
  56. }
  57. return self;
  58. }
  59. -(MKNetworkOperation*) operationWithPath:(NSString*) path {
  60. MKNetworkOperation *op = [super operationWithPath:path];
  61. op.postDataEncoding = MKNKPostDataEncodingTypeJSON;
  62. op.stringEncoding = NSUTF8StringEncoding;
  63. return op;
  64. }
  65. -(MKNetworkOperation*) operationWithPath:(NSString*) path
  66. params:(NSDictionary*) body {
  67. MKNetworkOperation *op = [super operationWithPath:path params:body];
  68. op.postDataEncoding = MKNKPostDataEncodingTypeJSON;
  69. op.stringEncoding = NSUTF8StringEncoding;
  70. return op;
  71. }
  72. -(MKNetworkOperation*) operationWithPath:(NSString*) path
  73. params:(NSDictionary*) body
  74. httpMethod:(NSString*)method {
  75. MKNetworkOperation *op = [super operationWithPath:path params:body httpMethod:method];
  76. op.postDataEncoding = MKNKPostDataEncodingTypeJSON;
  77. op.stringEncoding = NSUTF8StringEncoding;
  78. return op;
  79. }
  80. -(void)showPrompt:(NSString *)msg{
  81. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  82. [alert show];
  83. }
  84. -(BOOL)checkError:(id)resp{
  85. if ([resp isKindOfClass:[NSDictionary class]]) {
  86. NSString *msg = [resp objectForKey:@"message"];
  87. if (msg && [msg isKindOfClass:[NSString class]] && msg.length > 0) {
  88. if (_showError) {
  89. [self showPrompt:msg];
  90. }
  91. return YES;
  92. }
  93. }
  94. return NO;
  95. }
  96. @end