GET:json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// 1.得到請求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封裝請求參數
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@
"userId"
] = [appDefault objectForKey:@
"userId"
];
params[@
"IMSI"
] = _IMSI.text;
params[@
"nickName"
] = _nickName.text;
params[@
"phoneNumber"
] = _phoneNumber.text;
NSString *url = [ServiceInfo stringByAppendingString:@
"http://103.238.80.57:8080/Vietnam/Application/bindingDevice"
];
// 3.發送GET請求
[mgr GET:url parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@
"%@"
,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@
"%@"
,error);
}];
|
POST:app
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// 1.得到請求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封裝請求參數
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@
"userId"
] = [appDefault objectForKey:@
"userId"
];
params[@
"IMSI"
] = _IMSI.text;
params[@
"nickName"
] = _nickName.text;
params[@
"phoneNumber"
] = _phoneNumber.text;
NSString *url = [ServiceInfo stringByAppendingString:@
"http://103.238.80.57:8080/Vietnam/Application/bindingDevice"
];
// 3.發送GET請求
[mgr POST:url parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@
"%@"
,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@
"%@"
,error);
}];
|
值得注意的是,AFNetworking默認是使用JSON格式發送和響應返回來的數據的,若是返回來的數據不是JSON,就須要設置一下請求管理者,下面請看代碼:url
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
|
// 1.得到請求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 由於傳遞過去和接收回來的數據都不是json類型的,因此在這裏要設置爲AFHTTPRequestSerializer和AFHTTPResponseSerializer
mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
// 請求
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
// 響應
// 2.封裝請求參數
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@
"userId"
] = [appDefault objectForKey:@
"userId"
];
params[@
"IMSI"
] = _IMSI.text;
params[@
"nickName"
] = _nickName.text;
params[@
"phoneNumber"
] = _phoneNumber.text;
NSString *url = [ServiceInfo stringByAppendingString:@
"http://103.238.80.57:8080/Vietnam/Application/bindingDevice"
];
// 3.發送GET請求
[mgr GET:url parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// 轉碼,若是返回的數據不是JSON,這裏必需要轉碼
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@
"%@"
,string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@
"%@"
,error);
}];
|