Iphone 如何使用AFNetworking连接网站

Iphone 如何使用AFNetworking连接网站,iphone,xcode,afnetworking,Iphone,Xcode,Afnetworking,对不起,我是初学者。 如何使用AFNetworking连接网站: 我要做的是: { "method": "authorize", "params": [ "100000202", "TestApp677"` ] } 到端点: 我阅读了《开始指南》并添加了库,但我不知道如何开始 我有: NSURL *url = [NSURL

对不起,我是初学者。 如何使用AFNetworking连接网站:

我要做的是:

{
              "method": "authorize",
              "params": [
                "100000202",
                "TestApp677"`
              ]
            }
到端点:

我阅读了《开始指南》并添加了库,但我不知道如何开始

我有:

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}
URLWithString:@]]

我得到一个错误:

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'.}

以下魔法可能会有所帮助:

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];
如果GET不起作用,请将其替换为POST

更新

如果参数上有异常字符,例如:@M031leAp1将参数@M031leAp1替换为encodeToPercentEscapeString@M031leAp1然后将以下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));
}

你走了多远?你看过《入门指南》了吗?您是否已将库添加到应用程序中?你提出要求了吗?你被卡在哪一部分?是的,阅读开始指南,我添加了库,但我不知道如何开始。开始指南解释了如何开始。你在《起步指南》中走了多远?您被卡在了哪一位?我不知道如何添加查询。开始指南告诉您使用AFHTTPClient。是否已执行此操作?问题是服务器返回了错误的内容类型,如果无法在服务器上修复,则应在服务器上修复此问题。请尝试以下操作:[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@text/html,nil]我收到此错误:error Domain=JKErrorDomain Code=-1意外标记,需要“{',”},“[',”]“,”,“,”:“,”真“,”假“,”空“,”字符串“,”数字“.UserInfo=0x6d47460{JKAtIndexKey=0,JKLineNumberKey=1,NSLocalizedDescription=意外标记,需要“{',”,”[',”,”:“,”真“,”假“,”空“,”字符串“,”数字“}”
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'.}
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];
// 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));
}