iT邦幫忙

0

如何修改可以讓我傳值到這一個 CLASS

想請教各位大大!請問該如何修改 可以讓使用者自行修改URL並傳入到這一個 CLASS?


public class RetrofitBuilder {

    private static final String BASE_URL = "http://"+"127.0.0.1"+"/api/";

    private final static OkHttpClient client = buildClient();
    private final static Retrofit retrofit = buildRetrofit(client);

    private static OkHttpClient buildClient(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();


                        Request.Builder builder = request.newBuilder()
                                .addHeader("Accept","application/json")
                                .addHeader("Connection","close");

                        request = builder.build();

                        return chain.proceed(request);
                    }
                });

        if (BuildConfig.DEBUG){
            builder.addNetworkInterceptor(new StethoInterceptor());
        }

        return builder.build();
    }

    private static Retrofit buildRetrofit(OkHttpClient client){
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(MoshiConverterFactory.create())
                .build();
    }

    public static <T> T createService(Class<T> service){
        return retrofit.create(service);
    }

    public static <T> T createServiceWithAuth(Class<T> service, final TokenManger tokenManger){

        OkHttpClient newClient = client.newBuilder().addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {

                Request request = chain.request();

                Request.Builder builder = request.newBuilder();

                if(tokenManger.getToken().getAccessToken() != null){
                    builder.addHeader("Authorization","Bearer " + tokenManger.getToken().getAccessToken());
                }
                request = builder.build();

                return chain.proceed(request);
            }
        }).authenticator(CustomAuthenticator.getInstance(tokenManger)).build();

        Retrofit newRetrofit =retrofit.newBuilder().client(newClient).build();
        return newRetrofit.create(service);

    }

    public static Retrofit getRetrofit() {
        return retrofit;
    }
}

看更多先前的討論...收起先前的討論...
dog830228 iT邦新手 1 級 ‧ 2017-12-07 11:23:00 檢舉
URL字串當 方法參數
VictorW iT邦新手 5 級 ‧ 2017-12-07 11:45:10 檢舉
dog830228大大請問是像這樣嗎?
private static String url;
private static String BASE_URL = getUrl(url);

在寫一個

public static String getUrl(String url) {
return "http://"+url+"/api/";
}

遇到一個問題 如果我都是呼叫 createService 這個方法,URL 一起傳進來。
變成

public static <T> T createService(Class<T> service,String url)

我該如何把 url 傳給 getUrl 方法?
dog830228 iT邦新手 1 級 ‧ 2017-12-07 11:52:04 檢舉
你上面的程式碼 為什麼都是使用static
用到static通常會有特殊性

是否可以使用Get或Set 方法 來讀改URL
你程式裡面在去呼叫此屬性
dog830228 iT邦新手 1 級 ‧ 2017-12-07 11:52:04 檢舉
VictorW iT邦新手 5 級 ‧ 2017-12-07 16:07:45 檢舉
謝謝dog830228 大大 哈哈哈 雖然還是沒有用出來! 謝謝喔!

尚未有邦友回答

立即登入回答