对不起,我是初学者
如何使用AFNetworking连接网站:
I have login: xxx
password: yyy
我必须做查询:
{
"method": "authorize",
"params": [
"100000202",
"TestApp677"`
]
}
到端点:http://qqqqq.com/authorize/?ver=2_01
我读了开始指南,我添加了库,但我不知道如何开始。
我有:NSURL *url = [NSURL URLWithString:@"http://qqqqq.com/mobileapi/authorize/?ver=2_01"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setAuthorizationHeaderWithUsername:@"xxx" password:@"yyy"];
下一步是什么?
更新
当我使用Jano的代码时,我收到此错误:
<code>
Error Domain=com.alamofire.networking.error Code=-1016 "Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html" UserInfo=0x6875ce0<br> {NSErrorFailingURLKey=http://qqqqq.com/authorize/?ver=2_01,
NSLocalizedDescription=Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html}
更新
我改2行
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL
URLWithString:@ “http://qqqqq.com/mobileapi”]];
和
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/authorize/?ver=2_01"
parameters:jsonDic];
我收到错误:
Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code in (200-299), got
404" UserInfo=0x68562b0 {NSErrorFailingURLKey=http://qqqqq.com/authorize/?ver=2_01,
NSLocalizedDescription=Expected status code in (200-299), got 404}
更新
我的代码如下:
<code>NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
[jsonDic setObject:@"authorize" forKey:@"method"];
[jsonDic setObject:[NSArray arrayWithObjects:@"100000202",@"TestApp677", nil] forKey:@"params"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://xxxxx.com"]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setAuthorizationHeaderWithUsername:@"mobileapi" password:@"M031leA#p1"];
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/mobileapi/authorize/?ver=2_01" parameters:jsonDic];
[client setParameterEncoding:AFJSONParameterEncoding];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(@"%@",JSON);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@",error);
}];
[client enqueueHTTPRequestOperation:operation];</code>
and i get error:
Error Domain=JKErrorDomain Code=-1 "Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'." UserInfo=0x6d47460 {JKAtIndexKey=0, JKLineNumberKey=1, NSLocalizedDescription=Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.}
<B>UPDATE</B>
----------
MY CODE IS A LOOK LIKE:
NSString* encodeToPercentEscapeString(NSString *string) {
return (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef) string,
NULL,
(CFStringRef) @"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
}
- (void)viewDidLoad
{
//NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
//[jsonDic setObject:@"authorize" forKey:@"method"];
//
[jsonDic setObject:[NSArray arrayWithObjects:@"100000202",@"TestApp677", nil] forKey:@"params"];
NSDictionary *jsonDic = [NSString stringWithFormat:@"{\"method\":\"authorize\",\"params\":\[\"100000202\",\"TestApp\"]}",nil];
NSLog(@"%@", jsonDic);
NSString *password = encodeToPercentEscapeString(@"M031leA#p1");
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://vemmaeurope.com"]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setAuthorizationHeaderWithUsername:@"mobileapi" password:password];
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/mobileapi/authorize/?ver=2_01" parameters:jsonDic];
[client setParameterEncoding:AFJSONParameterEncoding];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(@"%@",JSON);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@",error);
}];
[client enqueueHTTPRequestOperation:operation];
// Do any additional setup after loading the view, typically from a nib.
}
并且我仍然有同样的错误:
Error Domain=JKErrorDomain Code=-1 "Unexpected token, wanted '{', '}', '[', ']', ',', ':',
'true', 'false', 'null', '"STRING"', 'NUMBER'." UserInfo=0x6c40cd0 {JKAtIndexKey=0,
JKLineNumberKey=1, NSLocalizedDescription=Unexpected token, wanted '{', '}', '[', ']', ',', ':',
'true', 'false', 'null', '"STRING"', 'NUMBER'.}
答案 0 :(得分:0)
以下结界可能会有所帮助:
NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
[jsonDic setObject:@"authorize" forKey:@"method"];
[jsonDic setObject:[NSArray arrayWithObjects:@"100000202",@"TestApp677", nil] forKey:@"params"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://qqqqq.com"]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setAuthorizationHeaderWithUsername:@"xxx" password:@"yyy"];
NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"/authorize/?ver=2_01" parameters:jsonDic];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(@"%@",JSON);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@",error);
}];
[client enqueueHTTPRequestOperation:operation];
如果不起作用,请用POST替换GET。
<强>更新强>
如果参数上有不寻常的字符,例如:@“M031leA#p1”用encodeToPercentEscapeString(@“M031leA#p1”)替换参数@“M031leA#p1”,然后将以下C函数粘贴到类中的任何位置:
// remove CFBridgingRelease and __bridge if your code is not ARC
NSString* encodeToPercentEscapeString(NSString *string) {
return (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef) string,
NULL,
(CFStringRef) @"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
}