-2

申し訳ありませんが、私は初心者です。
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'.}
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 に置き換えます。

アップデート

@"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 に答える