문제

I try to make an api call:

api.app.com/foo/search/Öhm

where Öhm is the search term. The problem is that this url causes a bad url exception, while normal chars work well. I tried with

[searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

However this produces a lot of % signs. The api expects Öhm. Any solution for this problem? I am using restkit.

The full error:

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x16e719d0 {NSUnderlyingError=0x16d70d10 "bad URL", NSLocalizedDescription=bad URL} 2014-04-03 18:26:55.404 My App[6844:3807] E restkit.network:RKObjectRequestOperation.m:243 GET '(null)' (0 / 0 objects) [request=0.0084s mapping=0.0000s total=0.0769s]: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x16e719d0 {NSUnderlyingError=0x16d70d10 "bad URL", NSLocalizedDescription=bad URL}

The termin is (null) instead of Öhm.

Code:

[[ApiSearchManager sharedManager] search:^(NSArray *result){
    //handle success
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    //handle fail - it fails for Öhm
} query:searchTerm];

and on the lower level:

- (void) search:(void (^)(NSArray *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure query:(NSString *)query{

    [self getObjectsAtPath:[NSString stringWithFormat:@"foo/search/%@", query] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        if (success) {
            success(mappingResult.array);
        }
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(operation, error);
        }
    }];
}
도움이 되었습니까?

해결책

The % signs are escaping, and that is how the URL should be transmitted. At the server side, the URL gets interpreted (unescaped) and the server-side code will see the special characters as you intended. For example it's very common to see URL's with %20 in them. ASCII character 20 (hex) is a space, so server-side code that obtains the URL (appropriately decoded) see a space in there.

다른 팁

This section of the log:

GET '(null)' (0 / 0 objects) [request=0.0084s mapping=0.0000s total=0.0769s]:

Means you're doing a GET, that the request URL is nil (which would be bad), so status code was obtained because no request was actually sent, it took a little time, blah blah

So, The URL loading system couldn't create a valid URL with which to make your request.

I'm going to guess that the only way you can achieve this is to not have a properly configured baseURL in your RKObjectManager. Check that. (though I would expect that to fail earlier - so you may be reduced to debugging around RKObjectRequestOperation.m, line 243, and back up the call stack)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top