okhttp3

Unexpected end of stream on okhttp3

妖精的绣舞 提交于 2019-12-09 05:59:40
问题 I had followed this link to refresh access token. While adding Authenticator to okHttp getting error of Unexpected end of stream on okhttp3 from retrofit call back onFailure method public class TokenAuthenticator implements Authenticator { AccessTokenRefreshModel accessTokenRefreshModel = null; @Override public Request authenticate(Route route, Response response) throws IOException { Call<UserLogin> call = iService.refreshAccessToken(BuildConfig.CLIENT_ID, refreshToken); UserLogin userLogin =

Create bearer authorization header in OkHttp java

眉间皱痕 提交于 2019-12-08 19:39:50
问题 I need to use OkHttp3 in java as a HTTP client and send Authorization header in request. example: Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRaswczovL2F1dGgucGF4aW11bS5djb20iLCJhdWQiOiJodHRwczovL2FwaS5wYXhpbXVtLmNvbSIsIm5iZiI6MTQ0ODQzNzkyMCwiZXhwIjoxNDQ4NDgxMTIwLCJzdWIiOiIzNzExZDk1YS03MWU1LTRjM2ItOWQ1YS03ZmY3MGI0NDgwYWMiLCJyb2xlIjoicGF4OmIyYjphcHA6dXNlciJ9.YR8Gs7RVM-q5AxtHpeOl2zYe-zKxh5u39TUeTbiZL1k how can I create this token using my username and password?

Access raw response body in Retrofit callback

余生颓废 提交于 2019-12-08 16:55:17
问题 Retrofit version: 2.1.0 OkHttp version: 3.4.1 In the onResponse method of my Retrofit Callback implementation, I have the following logic: @Override public void onResponse(Call<BaseResponseBody<T>> call, Response<BaseResponseBody<T>> response) { if (response != null && response.isSuccessful() && response.body() != null && response.body().containsValidSuccess()) { // Perform actions using response.body().getValue(). } else { // If containsValidSuccess returns false, show a dialog that includes

Exception Handling In Android from Web API

 ̄綄美尐妖づ 提交于 2019-12-08 11:44:51
问题 My code in MVC API C# this: [System.Web.Http.HttpPost] [System.Web.Http.Route("api/ServiceV1/Test")] public IHttpActionResult Test() { return BadRequest("Catch this message in Android application"); } Result in PostMan { "Message": "Catch this message in Android application" } I catch this error message on android application. I used okhttp3. String MIME_JSON = "application/json"; Gson gson = new Gson(); RequestBody body = RequestBody.create(MediaType.parse(MIME_JSON), gson.toJson(object));

How to fix Expected Android API level 21+ but was 19 in Android

大憨熊 提交于 2019-12-08 04:24:08
问题 In my application i want get data from server, for get connect to server i used Retrofit , OkHttp . But when running application, show me force close error. In android api 21+ is not error, but below api 21 show me force close error. ApiClient class codes: class ApiClient constructor(private val deviceUUID: String) { private val apiServices: ApiServices init { //Gson val gson = GsonBuilder() .setLenient() .create() //Http log val loggingInterceptor = HttpLoggingInterceptor()

How to set MockWebServer port to WebClient in JUnit test?

允我心安 提交于 2019-12-07 20:40:00
问题 I'm using spring-boot with WebClient , which is autowired as a bean. Problem: when writing a junit integration test, I have to use okhttp MockWebServer . This mock always starts up on a random port, eg localhost:14321 . Now my WebClient of course has a fixed url that it sends the requests to. This url may be given by an application.properties parameter like webclient.url=https://my.domain.com/ , so I could override that field in a junit test. But only statically. Question: how can I reset the

Okhttp ignores Dispatcher setting when used with Retrofit RxJavaCallAdapterFactory

…衆ロ難τιáo~ 提交于 2019-12-07 03:26:54
问题 Consider the following initializations of OkHttp and Retrofit: public static SomeServiceRestInterface newRestService(String apiUrl) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(apiUrl) .client(createOkHttpClient()) .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())) .addConverterFactory(createGsonConverter()) .build(); return retrofit.create(SomeServiceRestInterface.class); } private static OkHttpClient createOkHttpClient() { Dispatcher dispatcher =

OKHttp Authenticator custom http code other than 401 and 407

萝らか妹 提交于 2019-12-06 12:49:28
问题 I have oauth token implemented on server side but upon Invalid token or Token expirey i am getting 200 http status code but in response body i have {"code":"4XX", "data":{"some":"object"} When i try to read string in interceptor i get okhttp dispatcher java.lang.illegalstateexception closed because response.body().string() must be called only once. Also i read from here Refreshing OAuth token using Retrofit without modifying all calls that we can use OkHttp Authenticator class but it works

How to set MockWebServer port to WebClient in JUnit test?

点点圈 提交于 2019-12-06 12:12:33
I'm using spring-boot with WebClient , which is autowired as a bean. Problem: when writing a junit integration test, I have to use okhttp MockWebServer . This mock always starts up on a random port, eg localhost:14321 . Now my WebClient of course has a fixed url that it sends the requests to. This url may be given by an application.properties parameter like webclient.url=https://my.domain.com/ , so I could override that field in a junit test. But only statically. Question: how can I reset the WebClient bean inside a @SpringBootTest so that it sends the requests always to my mock server?

OKHTTP newCall() hangs when calling restAPI

試著忘記壹切 提交于 2019-12-06 11:27:36
Trying to post to a restAPI using JAVA OKHttp. My code looks as follows: try { loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .addInterceptor(loggingInterceptor) .build(); MediaType mediaType = MediaType.parse("app.lication/json"); RequestBody body = RequestBody.create(mediaType, FileUtils.readFileToString(file)); Request request = new Request.Builder() .url(restAPIUrl) .post(body) .addHeader("content-type", "application/json") .addHeader("cache-control"