Skip to content

Commit e20a3e8

Browse files
committed
Operation,网络请求封装
1 parent 0eb96b1 commit e20a3e8

File tree

14 files changed

+413
-1
lines changed

14 files changed

+413
-1
lines changed

NewsReader.xcodeproj/project.pbxproj

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
A49131391BCA8C0800511F97 /* AdvertInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = A49131381BCA8C0800511F97 /* AdvertInfo.m */; settings = {ASSET_TAGS = (); }; };
2020
A491313C1BCA8E8100511F97 /* ColumnInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = A491313B1BCA8E8100511F97 /* ColumnInfo.m */; settings = {ASSET_TAGS = (); }; };
2121
A491313F1BCA8EE000511F97 /* NewsInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = A491313E1BCA8EE000511F97 /* NewsInfo.m */; settings = {ASSET_TAGS = (); }; };
22+
A49131441BCA97DD00511F97 /* BaseOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = A49131431BCA97DD00511F97 /* BaseOperation.m */; settings = {ASSET_TAGS = (); }; };
23+
A49131471BCAB12F00511F97 /* GetAdvert.m in Sources */ = {isa = PBXBuildFile; fileRef = A49131461BCAB12F00511F97 /* GetAdvert.m */; settings = {ASSET_TAGS = (); }; };
24+
A491314A1BCAB1D600511F97 /* GetColumn.m in Sources */ = {isa = PBXBuildFile; fileRef = A49131491BCAB1D600511F97 /* GetColumn.m */; settings = {ASSET_TAGS = (); }; };
25+
A491314D1BCAB29500511F97 /* GetNews.m in Sources */ = {isa = PBXBuildFile; fileRef = A491314C1BCAB29500511F97 /* GetNews.m */; settings = {ASSET_TAGS = (); }; };
2226
A4C75B991BC80C08004264AA /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A4C75B981BC80C08004264AA /* main.m */; };
2327
A4C75B9C1BC80C08004264AA /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A4C75B9B1BC80C08004264AA /* AppDelegate.m */; };
2428
A4C75B9F1BC80C08004264AA /* HomeController.m in Sources */ = {isa = PBXBuildFile; fileRef = A4C75B9E1BC80C08004264AA /* HomeController.m */; };
@@ -48,6 +52,15 @@
4852
A491313B1BCA8E8100511F97 /* ColumnInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ColumnInfo.m; sourceTree = "<group>"; };
4953
A491313D1BCA8EE000511F97 /* NewsInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewsInfo.h; sourceTree = "<group>"; };
5054
A491313E1BCA8EE000511F97 /* NewsInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewsInfo.m; sourceTree = "<group>"; };
55+
A49131401BCA971400511F97 /* UrlDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UrlDefines.h; sourceTree = "<group>"; };
56+
A49131421BCA97DD00511F97 /* BaseOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BaseOperation.h; sourceTree = "<group>"; };
57+
A49131431BCA97DD00511F97 /* BaseOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BaseOperation.m; sourceTree = "<group>"; };
58+
A49131451BCAB12F00511F97 /* GetAdvert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GetAdvert.h; sourceTree = "<group>"; };
59+
A49131461BCAB12F00511F97 /* GetAdvert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GetAdvert.m; sourceTree = "<group>"; };
60+
A49131481BCAB1D600511F97 /* GetColumn.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GetColumn.h; sourceTree = "<group>"; };
61+
A49131491BCAB1D600511F97 /* GetColumn.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GetColumn.m; sourceTree = "<group>"; };
62+
A491314B1BCAB29500511F97 /* GetNews.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GetNews.h; sourceTree = "<group>"; };
63+
A491314C1BCAB29500511F97 /* GetNews.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GetNews.m; sourceTree = "<group>"; };
5164
A4C75B941BC80C08004264AA /* NewsReader.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NewsReader.app; sourceTree = BUILT_PRODUCTS_DIR; };
5265
A4C75B981BC80C08004264AA /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
5366
A4C75B9A1BC80C08004264AA /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
@@ -87,6 +100,21 @@
87100
path = JSONFramework;
88101
sourceTree = "<group>";
89102
};
103+
A49131411BCA979A00511F97 /* Operation */ = {
104+
isa = PBXGroup;
105+
children = (
106+
A49131421BCA97DD00511F97 /* BaseOperation.h */,
107+
A49131431BCA97DD00511F97 /* BaseOperation.m */,
108+
A49131451BCAB12F00511F97 /* GetAdvert.h */,
109+
A49131461BCAB12F00511F97 /* GetAdvert.m */,
110+
A49131481BCAB1D600511F97 /* GetColumn.h */,
111+
A49131491BCAB1D600511F97 /* GetColumn.m */,
112+
A491314B1BCAB29500511F97 /* GetNews.h */,
113+
A491314C1BCAB29500511F97 /* GetNews.m */,
114+
);
115+
path = Operation;
116+
sourceTree = "<group>";
117+
};
90118
A4C75B8B1BC80C08004264AA = {
91119
isa = PBXGroup;
92120
children = (
@@ -107,6 +135,7 @@
107135
A4C75B961BC80C08004264AA /* NewsReader */ = {
108136
isa = PBXGroup;
109137
children = (
138+
A49131411BCA979A00511F97 /* Operation */,
110139
A4C75BB51BC80C83004264AA /* Global */,
111140
A4C75BB41BC80C72004264AA /* Controller */,
112141
A4C75BB31BC80C6A004264AA /* Model */,
@@ -200,6 +229,7 @@
200229
isa = PBXGroup;
201230
children = (
202231
A49131331BCA5D4500511F97 /* Defines.h */,
232+
A49131401BCA971400511F97 /* UrlDefines.h */,
203233
);
204234
path = Global;
205235
sourceTree = "<group>";
@@ -283,13 +313,17 @@
283313
isa = PBXSourcesBuildPhase;
284314
buildActionMask = 2147483647;
285315
files = (
316+
A491314D1BCAB29500511F97 /* GetNews.m in Sources */,
286317
A49131301BCA531C00511F97 /* JsonParser.m in Sources */,
287318
A4C75B9F1BC80C08004264AA /* HomeController.m in Sources */,
288319
A49131361BCA63F600511F97 /* BaseInfo.m in Sources */,
289320
A4C75B9C1BC80C08004264AA /* AppDelegate.m in Sources */,
321+
A491314A1BCAB1D600511F97 /* GetColumn.m in Sources */,
322+
A49131441BCA97DD00511F97 /* BaseOperation.m in Sources */,
290323
89E2C3AF1BC92518001C1D01 /* README.md in Sources */,
291324
A491313C1BCA8E8100511F97 /* ColumnInfo.m in Sources */,
292325
A4C75B991BC80C08004264AA /* main.m in Sources */,
326+
A49131471BCAB12F00511F97 /* GetAdvert.m in Sources */,
293327
A49131321BCA531C00511F97 /* JsonWriter.m in Sources */,
294328
A491312F1BCA531C00511F97 /* JsonBase.m in Sources */,
295329
A49131311BCA531C00511F97 /* JsonUtility.m in Sources */,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
<Breakpoints>
6+
<BreakpointProxy
7+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
8+
<BreakpointContent
9+
shouldBeEnabled = "Yes"
10+
ignoreCount = "0"
11+
continueAfterRunningActions = "No"
12+
filePath = "NewsReader/Operation/BaseOperation.h"
13+
timestampString = "466262450.876171"
14+
startingColumnNumber = "9223372036854775807"
15+
endingColumnNumber = "9223372036854775807"
16+
startingLineNumber = "13"
17+
endingLineNumber = "13"
18+
landmarkName = "-executeOp"
19+
landmarkType = "4">
20+
</BreakpointContent>
21+
</BreakpointProxy>
22+
</Breakpoints>
23+
</Bucket>

NewsReader/Global/UrlDefines.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// UrlDefines.h
3+
// NewsReader
4+
//
5+
// Created by 唐有欢 on 15/10/11.
6+
// Copyright © 2015年 tangtech. All rights reserved.
7+
//
8+
9+
// 0正式版,1测试版
10+
#define ProductType 1
11+
12+
// 正式版自动使用正式环境
13+
#ifdef OFFICIAL
14+
#undef ProductType
15+
#define ProductType 0
16+
#endif
17+
18+
19+
#if ProductType == 0
20+
#define BaseHost @"http://www.tangtech.com"
21+
#define BasePort @""
22+
#else
23+
#define BaseHost @"http://127.0.0.1"
24+
#define BasePort @":80"
25+
#endif
26+
27+
#define BaseServer BaseHost BasePort
28+
#define BaseURLPath "/NewsReader/"
29+
#define BaseURL BaseServer BaseURLPath
30+
#define BaseServer2 @"http://c.m.163.com/nc/article/"
31+
32+
#define LoginURL BaseURL "login.json"
33+
#define AdvertURL BaseURL"/advert.json?width=%ld&height=%ld"
34+
#define ColumnURL BaseURL "column.json"
35+
#define NewsURLFmt BaseURL "news_%@.json"
36+
#define DetailURLFmt BaseServer2 "%@/full.html"
37+
#define ChartURL BaseURL "chart.json"
38+
#define H5URL BaseURL "H5/content.html"
39+
40+
#define CityCodeURLFmt @"http://apistore.baidu.com/microservice/cityinfo?cityname=%@"
41+
42+
// k78 URL
43+
#define WeatherFmt @"&format=json"
44+
#define WeatherKey @"10003"
45+
#define WeatherSign @"b59bc3ef6191eb9f747dd4e83c99f2a4"
46+
#define WeatherHost @"http://api.k780.com:88/"
47+
#define WeatherRealFmt WeatherHost "?app=weather.today&weaid=%@&appkey=" WeatherKey "&sign=" WeatherSign WeatherFmt
48+

NewsReader/Model/ColumnInfo.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// ColumnInfo.m
33
// NewsReader
44
//

NewsReader/NewsReaderPrefixHeader.pch

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
#ifdef __OBJC__
1010

1111
#import "Defines.h"
12+
#import "UrlDefines.h"
13+
#import "JsonUtility.h"
1214

1315
#endif /* NewsReaderPrefixHeader_pch */

NewsReader/Operation/BaseOperation.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// BaseOperation.h
3+
// NewsReader
4+
//
5+
// Created by 唐有欢 on 15/10/11.
6+
// Copyright © 2015年 tangtech. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
@protocol BaseOperationDelegate;
11+
@interface BaseOperation : NSObject{//声明变量,不能通过点语法设置和获取变量
12+
id<BaseOperationDelegate> _delegate;
13+
NSURLConnection *_connection;
14+
NSMutableData *_receiveData;//NSMutableData可变的字节操作类
15+
NSInteger _statusCode;
16+
long long _totalLength;//双长整型
17+
//默认是@protected
18+
@public NSDictionary *_opInfo;
19+
}
20+
21+
-(id)initWithDelegate:(id<BaseOperationDelegate>) delegate opInfo:(NSDictionary *) opInfo;
22+
-(NSMutableURLRequest *) urlRequest;
23+
- (void)executeOp;
24+
- (void)cancelOp;
25+
- (void)parseData:(id)data;
26+
- (void)parseSuccess:(NSDictionary *)dict jsonString:(NSString *)jsonString;
27+
- (void)parseFail:(id)dict;
28+
- (void)parseProgress:(long long)receivedLength;
29+
- (NSTimeInterval)timeoutInterval;
30+
31+
@end
32+
33+
34+
@protocol BaseOperationDelegate <NSObject>
35+
36+
-(void)opSuccess:(id)data;
37+
-(void)opFail:(NSString *) errorMsg;
38+
39+
@optional
40+
- (void)opSuccessEx:(id)data opinfo:(NSDictionary *)dictInfo;
41+
- (void)opFailEx:(NSString *)errorMessage opinfo:(NSDictionary *)dictInfo;
42+
- (void)opSuccessMatch:(id)data;
43+
- (void)opUploadSuccess;
44+
45+
@end

NewsReader/Operation/BaseOperation.m

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
//
2+
// BaseOperation.m
3+
// NewsReader
4+
//
5+
// Created by 唐有欢 on 15/10/11.
6+
// Copyright © 2015年 tangtech. All rights reserved.
7+
//
8+
9+
#import "BaseOperation.h"
10+
11+
@implementation BaseOperation
12+
13+
-(id)initWithDelegate:(id<BaseOperationDelegate>)delegate opInfo:(NSDictionary *)opInfo
14+
{
15+
if(self = [super init]){
16+
_delegate = delegate;
17+
_opInfo = opInfo;
18+
_totalLength = 0;
19+
}
20+
return self;
21+
}
22+
23+
- (void)cancelOp
24+
{
25+
if (_connection != nil) {
26+
BASE_INFO_FUN(@"_connection dealloc cancel");
27+
[_connection cancel];
28+
}
29+
_connection = nil;
30+
}
31+
32+
- (void)dealloc
33+
{
34+
if (_connection != nil) {
35+
BASE_INFO_FUN(@"_connection dealloc cancel");
36+
[_connection cancel];
37+
}
38+
_connection = nil;
39+
_delegate = nil;
40+
}
41+
42+
- (NSTimeInterval)timeoutInterval
43+
{
44+
return FxRequestTimeout;
45+
}
46+
47+
-(NSMutableURLRequest *)urlRequest
48+
{
49+
NSString *urlString = [_opInfo objectForKey:@"url"];
50+
BASE_INFO_FUN(urlString);
51+
52+
id body = [_opInfo objectForKey:@"body"];
53+
NSURL *url = [NSURL URLWithString:urlString];
54+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
55+
if (body != nil) {
56+
[request setHTTPMethod:HTTPPOST];
57+
if ([body isKindOfClass:[NSString class]]) {
58+
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
59+
}else{
60+
[request setHTTPBody:body];
61+
}
62+
}else{
63+
[request setHTTPMethod:HTTPGET];
64+
}
65+
66+
return request;
67+
68+
}
69+
70+
- (void)executeOp
71+
{
72+
_connection = [[NSURLConnection alloc] initWithRequest:[self urlRequest] delegate:self];
73+
}
74+
75+
76+
- (void)parseData:(NSData *)data
77+
{
78+
if (data.length == 0) {
79+
[self parseSuccess:nil jsonString:nil];
80+
return;
81+
}
82+
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
83+
NSMutableDictionary *dict = [JsonUtility jsonValueFromString:jsonString];
84+
NSString *result = [dict objectForKey:NetResult];
85+
if ([result isEqualToString:NetOk]) {
86+
[self parseSuccess:dict jsonString:jsonString];
87+
}else{
88+
[self parseFail:dict];
89+
}
90+
_receiveData = nil;
91+
92+
}
93+
- (void)parseSuccess:(NSDictionary *)dict jsonString:(NSString *)jsonString
94+
{
95+
[_delegate opSuccess:dict];
96+
}
97+
- (void)parseFail:(id)dict
98+
{
99+
if ([dict isKindOfClass:[NSString class]]) {
100+
[_delegate opFail:(NSString *)dict];
101+
return;
102+
}
103+
if ([[dict objectForKey:NetResult] isEqualToString:NetInvalidateToken]) {
104+
BASE_ERROR_FUN(NetInvalidateToken);
105+
}
106+
[_delegate opFail:[dict objectForKey:NetMessage]];
107+
}
108+
- (void)parseProgress:(long long)receivedLength
109+
{
110+
111+
}
112+
113+
#pragma mark NSURLConnectionDelegage methods
114+
115+
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse
116+
{
117+
NSHTTPURLResponse *response = (NSHTTPURLResponse *)aResponse;
118+
NSString *statusCode = [NSString stringWithFormat:@"%ld",(long)[response statusCode]];
119+
120+
_statusCode = [statusCode intValue];
121+
if (_statusCode == 200 || _statusCode == 206) {
122+
_totalLength = [response expectedContentLength];
123+
}
124+
BASE_INFO_FUN(statusCode);
125+
}
126+
127+
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data
128+
{
129+
BASE_INFO_FUN(([NSString stringWithFormat:@"%lu", (unsigned long)data.length]));
130+
[_receiveData appendData:data];
131+
[self parseProgress:_receiveData.length];
132+
}
133+
134+
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn
135+
{
136+
BASE_INFO_FUN([[NSString alloc] initWithData:_receiveData encoding:NSUTF8StringEncoding]);
137+
138+
// 成功接受:200有数据,204没有数据,206断点续传
139+
if (_statusCode == 200 || _statusCode == 204 || _statusCode == 206) {
140+
[self parseData:_receiveData];
141+
}
142+
else {
143+
144+
NSString *errorMessage = [[NSString alloc] initWithData:_receiveData encoding:NSUTF8StringEncoding];
145+
146+
if (errorMessage.length <= 0) {
147+
errorMessage = [[NSString alloc] initWithFormat:@"ResponseCode:%ld", (long)_statusCode];
148+
}
149+
150+
[self parseFail:errorMessage];
151+
}
152+
153+
_connection = nil;
154+
_receiveData = nil;
155+
}
156+
157+
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error
158+
{
159+
[self parseFail:[error localizedDescription]];
160+
161+
_connection = nil;
162+
_receiveData = nil;
163+
}
164+
165+
@end

NewsReader/Operation/GetAdvert.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// GetAdvert.h
3+
// NewsReader
4+
//
5+
// Created by 唐有欢 on 15/10/11.
6+
// Copyright © 2015年 tangtech. All rights reserved.
7+
//
8+
9+
#import "BaseOperation.h"
10+
11+
@interface GetAdvert : BaseOperation
12+
13+
@end

0 commit comments

Comments
 (0)