okhttp

feign使用教程

旧街凉风 提交于 2019-12-22 13:54:06
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 简介 Feign是一款java的Restful客户端组件,Feign使得 Java HTTP 客户端编写更方便。Feign 灵感来源于Retrofit, JAXRS-2.0和WebSocket。Feign 最初是为了降低统一绑定Denominator 到 HTTP API 的复杂度,不区分是否支持 ReSTfulness。 为什么选择Feign而不是其他 你可以使用 Jersey 和 CXF 这些来写一个 Rest 或 SOAP 服务的java客服端。你也可以直接使用 Apache HttpClient 来实现。但是 Feign 的目的是尽量的减少资源和代码来实现和 HTTP API 的连接。通过自定义的编码解码器以及错误处理,你可以编写任何基于文本的 HTTP API。 Feign工作机制 Feign 通过注解注入一个模板化请求进行工作。只需在发送之前关闭它,参数就可以被直接的运用到模板中。然而这也限制了 Feign,只支持文本形式的API,它在响应请求等方面极大的简化了系统。同时,它也是十分容易进行单元测试的。 基本用法 基本的使用如下所示,一个对于canonical Retrofit sample的适配。 interface GitHub { @RequestLine("GET /repos/{owner

How can I extract the raw JSON string from an OkHttp Response object?

醉酒当歌 提交于 2019-12-22 09:46:53
问题 private static final String URL = "http://www.livroandroid.com.br/livro/carros/carros_{tipo}.json"; public static List<Carro> getCarros(Context context, String tipo) throws IOException { String url = URL.replace("{tipo}", tipo); OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .url(URL) .build(); Response response = okHttpClient.newCall(request).execute(); String json = response.body().toString(); List<Carro> carros = parserJSON(context, json); return

Unable to close ChunkedInputStream quickly on Android 4.4 KitKat

冷暖自知 提交于 2019-12-22 09:41:58
问题 I've got an Android Daydream that displays a stream of tweets using Twitter4j's streaming implementation. This works fine on Android 4.2 and 4.3. On 4.4 however, I cannot quickly close the stream (in onDreamingStopped ). I'm getting this stacktrace but the NetworkOnMainThreadException isn't the problem. The problem appears to be related to this issue, around connection reuse. This OkHttp changeset (which gets merged into Android here) changes the way that close behaves on ChunkedInputStream .

okHttp3 java.lang.NoSuchMethodError: No virtual method setCallWebSocket

拥有回忆 提交于 2019-12-22 09:26:51
问题 I've updated from okhttp + retrofit to okhttp3 + retrofit2 but my app doesn't start cause of this Exception FATAL EXCEPTION: EventThread Process: appli.speaky.com, PID: 14705 java.lang.NoSuchMethodError: No virtual method setCallWebSocket(Lokhttp3/Call;)V in class Lokhttp3/internal/Internal; or its super classes (declaration of 'okhttp3.internal.Internal' appears in /data/data/appli.example.com/files/instant-run/dex/slice-okhttp-3.6.0_29bb10e5f590229716bfa8daf196d09ea67f56a8-classes.dex) at

Adding a custom certificate to an OkHttp Client

我是研究僧i 提交于 2019-12-22 06:09:12
问题 I am trying to make Android app, where I can get and parse HTML (from site which doesnt have API). I am using OkHttp. The site has untrusted (but valid) certificate. I am getting: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. I've already set up the official way (https://developer.android.com/training/articles/security-ssl#java) and now I need to link it with OkHttpClient. I tried OkHttpClient client = new OkHttpClient; OkHttpClient.Builder

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"))

Using OkHttp client via OKClient on Google App Engine throws a “java.lang.NoClassDefFoundError: java.net.ProxySelector” is a restricted class error

自作多情 提交于 2019-12-22 04:04:15
问题 I am trying to use OKHTTP (version 2.4.0) along retrofit (1.9.0) on google app engine (1.9.22). Here is the how i use it: OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(COMPOSER_MODULE_CONNECTION_TIMEOUT, TimeUnit.SECONDS); okHttpClient.setReadTimeout(COMPOSER_MODULE_SOCKET_TIMEOUT, TimeUnit.SECONDS); RestAdapter restAdapter = new RestAdapter.Builder() .setLogLevel(RestAdapter.LogLevel.FULL) .setConverter(new JacksonConverter()) .setEndpoint(ENDPOINT_PATH)

How to implement MockWebServer to work with proxy

半腔热情 提交于 2019-12-22 00:25:43
问题 I am trying to implement a MockWebServer from Square and i am behind a proxy. The problem is that every time i am executing my instrumentation test will fail because i am getting a 407 for every request i am doing to my MockWebServer. debug.level.titleD/OkHttp: <-- 407 Proxy Authentication Required http://localhost:12345/user/login (767ms) As u see i am pointing to my localhost and i dont know why i am getting this! Here is my MockWebServer implementation! public class MockedTestServer {

HTTP/2 with OkHttp

纵饮孤独 提交于 2019-12-21 16:59:17
问题 I am trying to communicate with a HTTP/2 server using OkHttp client. Added to Maven POM: <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.2.0</version> </dependency> And this is my test code: OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("https://http2.akamai.com/demo").build(); Response response = client.newCall(request).execute(); System.out.println("Protocol: " + response.protocol()); System.out.println

NetworkOnMainThread RxJava + Retrofit + Lollipop+

我与影子孤独终老i 提交于 2019-12-21 12:17:08
问题 I'm getting reports of a NetworkOnMainThread exception on Lollipop+ when running an https api call using Retrofit and RxAndroid . I have isolated the code and created the following example that still fails. Here's the build.gradle : apply plugin: 'com.android.application' apply plugin: 'me.tatarka.retrolambda' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.example.bug" minSdkVersion 9 targetSdkVersion 23 versionCode 1 versionName "1.0" }