-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonLoaderInternal.m
180 lines (120 loc) · 4.29 KB
/
JsonLoaderInternal.m
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
``The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
The Initial Developer of the Original Code is Hollrr, LLC.
Portions created by the Initial Developer are Copyright (C) 2011
the Initial Developer. All Rights Reserved.
Contributor(s):
Dustin Dettmer <[email protected]>
*/
#import "JsonLoaderInternal.h"
#import "CJSONDeserializer.h"
#import "Util.h"
@interface JsonLoaderInternal ()
-(void)dumpResponse;
@property (nonatomic, retain) NSURLConnection *connection;
@property (nonatomic, retain) NSMutableData *requestData;
@property (nonatomic, readwrite, retain) NSURLResponse *response;
@end
@implementation JsonLoaderInternal
@synthesize delegate, response;
@synthesize connection, requestData;
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)del {
if(self = [super init]) {
self.delegate = del;
if ([Util isNotEmpty:request.URL]) {
self.connection = [NSURLConnection
connectionWithRequest:request
delegate:self];
}
else {
DLog(@"jsonLoaderInternal.initWithRequest called with empty url");
if([self.delegate respondsToSelector:@selector(jsonFailed:)])
[self.delegate jsonFailed:nil];
}
}
return self;
}
- (void)showError:(NSString*)errorStr json:(NSString*)json {
if(errorStr) {
if([self.delegate respondsToSelector:@selector(willShowError:error:json:)])
if(![self.delegate willShowError:nil error:errorStr json:json])
return;
/* this should be disabled by default, but a developer could enable these types of debug alerts by setting a #define
disabling until we have that in place.
UIAlertView *alert =
[[UIAlertView alloc]
initWithTitle:@"Issue"
message:errorStr
delegate:nil
cancelButtonTitle:@"Alright"
otherButtonTitles:nil];
[alert show];
[alert release];
*/
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)res {
self.response = res;
self.requestData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.requestData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSHTTPURLResponse *hRes = nil;
if([self.response isKindOfClass:[NSHTTPURLResponse class]])
hRes = (NSHTTPURLResponse*)self.response;
if(hRes && hRes.statusCode != 200) {
[self dumpResponse];
if(hRes.statusCode == 401 && [self.delegate respondsToSelector:@selector(jsonFailedWithAuthError:)]) {
[self.delegate jsonFailedWithAuthError:nil];
}
else if ([self.delegate respondsToSelector:@selector(jsonFailed:)]) {
[self.delegate jsonFailed:nil];
}
}
else
[self.delegate performSelector:@selector(didFinishLoading:)
withObject:self.requestData];
self.connection = nil;
self.requestData = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"JsonLoaderError: %@", error.localizedDescription);
[self showError:[NSString stringWithFormat:@"Can't get internet.\n\n%@",
[error localizedDescription]] json:nil];
if([self.delegate respondsToSelector:@selector(jsonFailed:)])
[self.delegate jsonFailed:nil];
self.connection = nil;
self.requestData = nil;
}
- (void)cancel {
if([self.delegate respondsToSelector:@selector(jsonCanceled)])
[self.delegate performSelector:@selector(jsonCanceled)];
[self.connection cancel];
self.connection = nil;
}
- (void)dealloc {
self.delegate = nil;
self.response = nil;
[self cancel];
self.connection = nil;
self.requestData = nil;
[super dealloc];
}
#pragma mark - Private
-(void)dumpResponse {
NSString* responseStr = [[NSString alloc] initWithData:self.requestData
encoding:NSUTF8StringEncoding];
NSRange stringRange = {0,100};
if ([responseStr length] > 100) {
NSLog(@"jsonFailed - response (truncated): %@", [responseStr substringWithRange:stringRange]);
}
else {
NSLog(@"jsonFailed - response: %@", responseStr);
}
}
@end