我正在尝试使用AFHTTPSessionManager向Web服务发送请求

这是我的问题:我想发送带有JSON格式参数的URL GET请求。
像这样:
http://api.mysite.com/v2/json/search?parameters= {“api_key”:“YOUR_API_
KEY”,“query”:{“perpage”:50}}

我已经将AFHTTPSessionManager子类化了。这是我的代码:

    + (MyAPIClient *)sharedClient {
        static MyAPIClient *_sharedClient = nil;
        static dispatch_once_t oncePredicate;
        dispatch_once(&oncePredicate, ^{
            _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kBaseURLString]];
        });
        return _sharedClient;
    }

和初始化器:
- (instancetype)initWithBaseURL:(NSURL *)url {

    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    self.responseSerializer = [AFJSONResponseSerializer serializer];
    self.requestSerializer = [AFJSONRequestSerializer serializer];
    return self;
}

所以这是我的做法:
- (void)sendMyQueryWithSuccess:
       (void(^)(NSURLSessionDataTask *task, id responseObject))success
        andFailure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure{


        NSDictionary *params = @{@"api_key" : kAPIKey,
                                @"query": @{@"perpage": @50}};

        NSString *paramsstring = [[NSString alloc]
            initWithData:[NSJSONSerialization
                dataWithJSONObject:params
                options:0
                error:nil]
            encoding:NSUTF8StringEncoding];

        NSString* path = [NSString stringWithFormat:@"search_sale?parameters=%@",paramsstring];

        [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            if (success) {
                success(task, responseObject);
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            if (failure) {
                failure(task, error);
            }
        }];
    }

我不断得到这个:

*-[AFJSONRequestSerializer requestWithMethod:URLString:parameters:error:],AFNetworking / AFNetworking / AFURLRequestSerialization.m:277中的断言失败

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效的参数不令人满意:URLString”

那么这是什么问题呢?

当我打印“path”的值时,我得到的正是我想要的:
search?parameters={"api_key":"SOME_API_KEY","query":{"perpage":10}}

非常感谢您的任何投入

最佳答案

是的你可以:

NSString *stringCleanPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

我必须将其用于parse.com的rest api和AFNetworking,并使其完美运行。

10-06 08:18