-2

对不起,我是初学者。
如何使用 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'.}
4

1 回答 1

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];

如果 GET 不起作用,请用 POST 替换 GET。

更新

如果您的参数中有不寻常的字符,例如:@"M031leA#p1" 将参数 @"M031leA#p1" 替换为 encodeToPercentEscapeString(@"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));
}
于 2012-08-02T14:29:17.123 回答