-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathASIHttpTestViewController.m
141 lines (105 loc) · 4.39 KB
/
ASIHttpTestViewController.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
//
// ASIHttpTestViewController.m
// ASIHttpTest
//
// Created by wxg on 13-7-10.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import "ASIHttpTestViewController.h"
@implementation ASIHttpTestViewController
//同步GET请求
-(void)httpGetSynchronousRequest
{
NSString *user = @"admin";
NSString *urlStr = [NSString stringWithFormat:@"http://127.0.0.1:8080/B/b?user=%@", user];
// NSURL *url = [NSURL URLWithString:urlStr];
NSURL *url = [NSURL URLWithString: @"http://192.168.1.106:8181/api/CoinAPI?cid=0&pid=0"];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
//设置超时时间
request.timeOutSeconds = 20;
request.delegate = self;
//发起一个同步请求 如果不手动发起请求,不会默认发起
[request startSynchronous];
}
//同步POST请求
-(void)httpPostSynchronousRequest
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:8080/B/b"]];
request.timeOutSeconds = 20;
request.delegate = self;
[request addPostValue:@"admin123" forKey:@"user"];
[request addPostValue:@"user@qq.com" forKey:@"email"];
[request startSynchronous];
}
//异步GTE请求
-(void)httpGetAsynchronousRequest
{
NSString *user = @"admin";
NSString *urlStr = [NSString stringWithFormat:@"http://127.0.0.1:8080/B/b?user=%@", user];
NSURL *url = [NSURL URLWithString:@"http://192.168.1.106:8181/api/CoinAPI?cid=0&pid=0"];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
//设置超时时间
request.timeOutSeconds = 20;
request.delegate = self;
//发起一个异步请求 如果不手动发起请求,不会默认发起
[request startAsynchronous];
}
//异步POST请求
-(void)httpPostAsynchronousRequest
{
// ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://125.0.0.176/greenload/test_wifi"]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.106:8181/api/CoinAPI?cid=0&pid=0"]];
request.timeOutSeconds = 20;
request.delegate = self;
[request addPostValue:@"admin123" forKey:@"user_id"];
//[request addPostValue:@"user@qq.com" forKey:@"email"];
[request startAsynchronous];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// [self httpGetSynchronousRequest];
[self httpGetAsynchronousRequest];
//[self httpPostSynchronousRequest];
// [self httpPostAsynchronousRequest];
}
-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
NSLog(@"%@", responseHeaders);
}
//说明:这个协议方法不要重写,ASI框架利用这个方法帮我们处理了数据,只需要在请求结束的时候获得数据即可
//如果重写这个方法需要你自己处理数据
//-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
//{
//}
-(void)requestFinished:(ASIHTTPRequest *)request
{
//[request responseString] 以字符串的形式展示返回的数据,如果返回的数据不是字符串,得到的结果会有问题,有的时候字符串存在编码问题,得到字符串可能是乱码,需要自己首先获得data然后用data创建字符串并指定编码方式为utf-8
//[request responseData] 以二进制数据的形式展示返回的数据
NSLog(@"这里是返回结果--->%@", [request responseString]);
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
NSString *result = [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding];
NSLog(@"result:%@",result);
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:[request responseData]
options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dict11:%@",dict);
label.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"BuyPrice"]];
[self.view addSubview:label];
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"请求失败!");
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end