android - How to add static parametr to selected GET Retrofit calls? -
i have use lot of retrofit calls (get,put,detele,etc.). know, can calls, must set calls. have add 1 static parametr calls, best way it?
my call examples:
@get("user") call<user> getuser(@header("authorization") string authorization) @get("group/{id}/users") call<list<user>> grouplist(@path("id") int groupid, @query("sort") string sort);
i need gets add parametr: &something=true
i tried add way, require fix calls interface:
public interface apiservice { string getparametervariable = "something" boolean getparametervalue = true @get("user") call<user> getuser(@header("authorization") string authorization, @query(getparametervariable) boolean getparametervalue) }
this answer assume using okhttp
retrofit
.
you have add interceptor okhttpclient
instance filters get
requests , apply query parameter. can in way:
// add new interceptor okhttpclient instance. okhttpclient.interceptors().add(new interceptor() { @override public okhttp3.response intercept(chain chain) throws ioexception { request request = chain.request(); // check method first. if (request.method().equals("get")) { httpurl url = request.url() .newbuilder() // add query parameter requests. .addqueryparameter("something", "true") .build(); request = request.newbuilder() .url(url) .build(); } // proceed chaining requests. return chain.proceed(request); } });
Comments
Post a Comment