使用两个不同的baseUrl-AFHTTPSessionManager [英] Working with two different baseUrl - AFHTTPSessionManager

查看:425
本文介绍了使用两个不同的baseUrl-AFHTTPSessionManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFHTTPSessionManager在网络上进行api调用。我有会话管理器的signleton对象,它会一次初始化基本url。有时,我需要使用不同的baseurl进行api调用。

在这里处理不同的baseurl的正确方法是什么?

  +(ApiClient *)sharedClient {

静态ApiClient * _sharedClient = nil;

static dispatch_once_t OncePredicate;

dispatch_once(& oncePredicate,^ {
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kTraktBaseURLString]];
});
return _sharedClient;
}


解决方案

您可以使用以下行为 + URLWithString:relativeToURL:方法来覆盖baseURL。



Matt 在文档中提到


对于HTTP便捷方法,请求序列化程序将构造URL如果提供,则使用NSURL + URLWithString:relativeToURL:从相对于baseURL的路径开始。如果baseURL为零,则路径需要使用NSURL + URLWithString:解析为有效的NSURL对象。



以下是baseURL和相对路径如何相互作用的一些示例:

  NSURL * baseURL = [NSURL URLWithString:@ http://example.com/v1/]; 
[NSURL URLWithString:@ foo relativeToURL:baseURL]; // http://example.com/v1/foo
[NSURL URLWithString:@ foo?bar = baz relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:@ / foo relativeToURL:baseURL]; // http://example.com/foo
[NSURL URLWithString:@ foo / relativeToURL:baseURL]; // http://example.com/v1/foo
[NSURL URLWithString:@ / foo / relativeToURL:baseURL]; // http://example.com/foo/
[NSURL URLWithString:@ http://example2.com/ relativeToURL:baseURL]; // http://example2.com/


因此,如果您要更改单个请求的baseURL,可以将绝对URL作为 URLString 参数传递给 GET:parameters:success:failure:而不是URL路径。

  [管理员GET:@ http://otherBaseURL.com/url/path参数:nil成功:...失败:...] 


I am using AFHTTPSessionManager to make api calls on the web. I have signleton object of session manager and it initiates the base url once. Occasionally, I am in a need of making api call with different baseurl. And its readonly in AFNetworking.h file.

What is the proper way of handing different baseurl here? Plese help.

+ (ApiClient *)sharedClient {

    static ApiClient *_sharedClient = nil;

    static dispatch_once_t oncePredicate;

    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kTraktBaseURLString]];
    });
    return _sharedClient;
}

解决方案

You can use the behaviour of +URLWithString:relativeToURL: method to override baseURL.

Matt mentioned it in Docs

For HTTP convenience methods, the request serializer constructs URLs from the path relative to the baseURL, using NSURL +URLWithString:relativeToURL:, when provided. If baseURL is nil, path needs to resolve to a valid NSURL object using NSURL +URLWithString:.

Below are a few examples of how baseURL and relative paths interact:

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
[NSURL URLWithString:@"foo" relativeToURL:baseURL];                  //  http://example.com/v1/foo
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];          // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:@"/foo" relativeToURL:baseURL];                 // http://example.com/foo
[NSURL URLWithString:@"foo/" relativeToURL:baseURL];                 // http://example.com/v1/foo
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL];                // http://example.com/foo/
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/

So if you want to alter baseURL for single request, you can pass Absolute URL as a URLString argument to GET:parameters:success:failure: instead of URL path.

[manager GET:@"http://otherBaseURL.com/url/path" parameters:nil success:... failure:...]

这篇关于使用两个不同的baseUrl-AFHTTPSessionManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆