申し訳ありませんが、私は初心者です。
AFNetworking を使用して Web サイトに接続する方法:
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'.}