okhttp3

how to get response body in okhttp when code is 401

跟風遠走 提交于 2019-12-22 04:18:23
问题 i am using okHttp 3.2.0 and here is code for building request object MediaType JSON = MediaType.parse(AppConstants.CONTENT_TYPE_VALUE_JSON); RequestBody body = RequestBody.create(JSON, requestBody); HttpUrl url = new HttpUrl.Builder() .scheme("http") .host("192.168.0.104") .port(8080) .addPathSegment("mutterfly-server") .addPathSegment("j_spring_security_check") .addQueryParameter("j_username", jsonObject.getString("emailId")) .addQueryParameter("j_password", jsonObject.getString("password"))

setLevel okhttp LoggingInterceptor deprecated

∥☆過路亽.° 提交于 2019-12-22 03:52:23
问题 setLevel(okhttp3.logging.HttpLoggingInterceptor.Level)' is deprecated what should replace with setLevel? to remove the deprecated issue 回答1: According to the documentation "Moved to var. Replace setLevel(...) with level(...) to fix Java", Replace setLevel(...) with level(...) will fix this issue example : HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.level(HttpLoggingInterceptor.Level.BODY); Happy coding :) 回答2: for Kotlin replace : val logger: HttpLoggingInterceptor

Send authorization header with every request in webview using okhttp in android

眉间皱痕 提交于 2019-12-21 17:07:14
问题 I am using WebView to display web page, but the server expects an authorization token with every request from my webview. Anybody know if this is possible ? I referred this post in #SO adding-header-to-all-request-with-retrofit-2. But I'm not able to get the result. This is my code (bare with my coding standard, I'm a beginner) public class TableViewTest extends AppCompatActivity { ScrollView scrollView; WebView webView; SharedPreferences pref; @SuppressLint("SetJavaScriptEnabled") @Override

How to add Basic Authentication in Picasso 2.5.2 with OkHttp 3.2.0

假装没事ソ 提交于 2019-12-21 16:22:40
问题 I am using the picasso 2.5.2 library to download the bitmap so in the api I need to pass the Basic Authentication in the headers. i have tried the following SO ansers but none of them work with the latest picasso and OkHttp libraries. Answer - 1 Answer - 2 Answer - 3 Thanks in advance. 回答1: Try configuring an OkHttp3 client with authenticator, depending on your scheme and situation: OkHttpClient okHttpClient = new OkHttpClient.Builder() .authenticator(new Authenticator() { @Override public

Detect if OkHttp response comes from cache (with Retrofit)

那年仲夏 提交于 2019-12-20 17:41:32
问题 Is there a way to detect if a Retrofit response comes from the configured OkHttp cache or is a live response? Client definition: Cache cache = new Cache(getCacheDirectory(context), 1024 * 1024 * 10); OkHttpClient okHttpClient = new OkHttpClient.Builder() .cache(cache) .build(); Api definition: @GET("/object") Observable<Result<SomeObject>> getSomeObject(); Example call: RetroApi retroApi = new Retrofit.Builder() .client(okHttpClient) .baseUrl(baseUrl) .addCallAdapterFactory

OkHttp proxy settings

自古美人都是妖i 提交于 2019-12-20 17:33:20
问题 I have to setup a proxy to send a JSON using POST, using proxyHost and proxyPort. public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); Proxy proxyTest = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy", proxyPort)); OkHttpClient client = new OkHttpClient() .proxy(proxyTest) .build(); //OkHttpClient.Builder builder = new OkHttpClient.Builder(); //builder.proxy(proxySAP); //client.setProxy(proxySAP) //OkHttpClient client = builder.build();; String post

Retrofit 2 HTTP method annotation is required (e.g., @GET, @POST, etc.)

十年热恋 提交于 2019-12-20 10:39:41
问题 What's wrong with my Retrofit configuration? I'm having this error when I'm adding Basic Authentication with my OkHttpClient but when I used the default client without Interceptor it's working. Or is there something wrong with my Gradle Dependencies..? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalArgumentException : HTTP method annotation is required (e.g., @GET, @POST, etc.). for method APIService.getRegAccrDetails at retrofit.Utils.methodError(Utils.java:177) at retrofit.Utils

OkHttp: avoid leaked connection warning

六眼飞鱼酱① 提交于 2019-12-20 10:22:53
问题 I am using OkHttp 3, and I keep getting leaked connection warnings: WARNING: A connection to https://help.helpling.com/ was leaked. Did you forget to close a response body? Jul 14, 2016 6:57:09 PM okhttp3.ConnectionPool pruneAndGetAllocationCount Everytime I get a ResponseBody , I either call .string() which supposedly closes the stream for me, or I explicitly close it in a finally block, in the following way: ResponseBody responseBody = response.body(); try (Reader responseReader =

What is the difference between the OkHttp methods .toString() and .string()?

狂风中的少年 提交于 2019-12-20 01:34:15
问题 I have a snippet of code: override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) { try { Log.d("DEBUG POST=====>", response.body()!!.string()) }catch(e:IOException) { e.printStackTrace() } } When I use response.body()!!.string() I get the correct output, and JSON body. When I use: response.body().toString() I get okhttp3.ResponseBody$1@c626d25 Can anyone kindly tell me what is the difference between the two methods? 回答1: string() isn't a valid Kotlin (or Java)

“Multipart body must have at least one part”

北城以北 提交于 2019-12-19 10:32:37
问题 In previous asked question (react-native upload pictures on android) described request with param "FormData", but I requested GET request without data and got same message! react-native app with ProGuard enabled = true App crashed on first request. 06-16 14:00:30.971: E/AndroidRuntime(14321): java.lang.IllegalStateException: Multipart body must have at least one part. 06-16 14:00:30.971: E/AndroidRuntime(14321): at okhttp3.MultipartBody$Builder.build(SourceFile:327) 06-16 14:00:30.971: E