Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Okhttpclient Create a Http Delete Or Put Method With Params? android okhttpclient

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();
like image 983
WU Sufun Avatar asked Jul 19 '16 17:07

WU Sufun


1 Answers

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.

like image 176
Bryan Avatar answered Sep 30 '22 09:09

Bryan