Does anyone know in Android How to use Okhttpclient Create a Http Delete Or Put Method With Params?
Using java , this is what i have tried:
CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(context));
okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS) //其他配置
.build();
You can build a URL with query parameters using the HttpUrl
class. Then you can use an okhttp3.Request.Buidler()
along with either the post()
or delete()
methods:
HttpUrl url = new HttpUrl.Builder()
.host(host).addQueryParameter(name, value).build();
Request request = new Request.Builder()
.url(url).post(RequestBody.create(mediaType, body)).addHeader(type, header).build();
okhttpClient.newCall(request).enqueue(new Callback() {
...
});
You can check out the OkHttp wiki for recipes if you need further help. Or you could use Square's other wonderful library, Retrofit, which pairs well with OkHttp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With