我目前正在尝试更多地利用 kotlin 协程。但我面临一个问题:在这些协程中使用 moshi 或 okhttp 时,我收到警告:
“不适当的阻塞方法调用”
解决这些问题的最佳方法是什么?我真的不想不合适;-)
我正在使用OkHttp库开发app,我的麻烦是我找不到如何设置连接超时和套接字超时.
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在尝试使用Retrofit和OKHttp来缓存HTTP响应.我按照这个要点,结束了这段代码:
File httpCacheDirectory = new File(context.getCacheDir(), "responses");
HttpResponseCache httpResponseCache = null;
try {
httpResponseCache = new HttpResponseCache(httpCacheDirectory, 10 * 1024 * 1024);
} catch (IOException e) {
Log.e("Retrofit", "Could not create http cache", e);
}
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setResponseCache(httpResponseCache);
api = new RestAdapter.Builder()
.setEndpoint(API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(new OkClient(okHttpClient))
.build()
.create(MyApi.class);
Run Code Online (Sandbox Code Playgroud)
这是带有Cache-Control标头的MyApi
public interface MyApi {
@Headers("Cache-Control: public, max-age=640000, s-maxage=640000 , max-stale=2419200")
@GET("/api/v1/person/1/")
void requestPerson(
Callback<Person> callback
);
Run Code Online (Sandbox Code Playgroud)
首先,我在线请求并检查缓存文件.有正确的JSON响应和标题.但是当我尝试离线请求时,我总是得到RetrofitError UnknownHostException
.我还有什么办法让Retrofit从缓存中读取响应吗?
编辑:
因为OKHttp 2.0.x HttpResponseCache
是Cache …
出于测试目的,我正在尝试将套接字工厂添加到我的okHttp客户端,该客户端在设置代理时信任所有内容.这已经完成了很多次,但是我对一个信任套接字工厂的实现似乎缺少了一些东西:
class TrustEveryoneManager implements X509TrustManager {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { }
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { }
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
OkHttpClient client = new OkHttpClient();
final InetAddress ipAddress = InetAddress.getByName("XX.XXX.XXX.XXX"); // some IP
client.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipAddress, 8888)));
SSLContext sslContext = SSLContext.getInstance("TLS");
TrustManager[] trustManagers = new TrustManager[]{new TrustEveryoneManager()};
sslContext.init(null, trustManagers, null);
client.setSslSocketFactory(sslContext.getSocketFactory);
Run Code Online (Sandbox Code Playgroud)
没有请求从我的应用程序发出,并且没有异常被记录,因此它似乎在okHttp中无声地失败.经过进一步调查,似乎Connection.upgradeToTls()
在强制握手时,okHttp中有一个异常被吞没.我得到的例外是:javax.net.ssl.SSLException: SSL handshake terminated: ssl=0x74b522b0: SSL_ERROR_ZERO_RETURN occurred. You …
使用OkHttp我们可以发出HTTP请求然后从服务器获得响应
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
Run Code Online (Sandbox Code Playgroud)
然后用Gson lib将响应转换为我们需要的对象.
这是来自Square/OkHttp doc:
它的请求/响应API设计具有流畅的构建器和不变性.它支持同步阻塞调用和带回调的异步调用
我从stackOverFlow中读取
如果可用,Retrofit会自动使用OkHTTP
.
所以我的问题是究竟是什么改造?
什么改造可以做到OkHttp不能?!
我认为OkHttp和Gson解决了请求API问题,那么Retrofit为我们解决了什么问题?
我读了一些将jsons发布到服务器的例子.
有人说:
OkHttp是Java提供的HttpUrlConnection接口的实现.它提供了用于编写内容的输入流,并且不知道(或关心)该内容的格式.
现在我想使用名称和密码的参数对URL进行正常发布.
这意味着我需要自己将名称和值对编码为流吗?
我有这个拦截器,我添加到我的OkHttp客户端:
public class RequestTokenInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// Here where we'll try to refresh token.
// with an retrofit call
// After we succeed we'll proceed our request
Response response = chain.proceed(request);
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在我的拦截器中添加标头以进行请求?
我尝试了这个,但我犯了错误,在创建新请求时我失去了我的请求:
public class RequestTokenInterceptor implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
Request newRequest;
try {
Log.d("addHeader", "Before");
String token = TokenProvider.getInstance(mContext).getToken();
newRequest = …
Run Code Online (Sandbox Code Playgroud) 我在我的Android应用程序中使用OKHTTP客户端进行网络连接.
此示例显示如何上载二进制文件.我想知道如何使用OKHTTP客户端获取二进制文件下载的输入流.
以下是该示例的列表:
public class InputStreamRequestBody extends RequestBody {
private InputStream inputStream;
private MediaType mediaType;
public static RequestBody create(final MediaType mediaType,
final InputStream inputStream) {
return new InputStreamRequestBody(inputStream, mediaType);
}
private InputStreamRequestBody(InputStream inputStream, MediaType mediaType) {
this.inputStream = inputStream;
this.mediaType = mediaType;
}
@Override
public MediaType contentType() {
return mediaType;
}
@Override
public long contentLength() {
try {
return inputStream.available();
} catch (IOException e) {
return 0;
}
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
Source source …
Run Code Online (Sandbox Code Playgroud) 我正在使用OkHttp库进行新项目,并且对其易用性印象深刻.我现在需要使用基本身份验证.不幸的是,缺乏工作示例代码.我正在寻找一个如何在遇到HTTP 401标头时将用户名/密码凭据传递给OkAuthenticator的示例.我看到了这个答案:
使用基本HTTP身份验证更改POST请求:"无法重试流式HTTP正文"
但它并没有让我走得太远.OkHttp github repo上的样本也没有基于身份验证的样本.有没有人有一个要点或其他代码示例让我指向正确的方向?谢谢你的协助!
我正在为拥有自签名SSL证书的服务器的客户工作.
我使用包装的OkHttp客户端使用Retrofit + CustomClient:
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Config.BASE_URL + Config.API_VERSION)
.setClient(new CustomClient(new OkClient(), context))
.build();
Run Code Online (Sandbox Code Playgroud)
OkHttp默认支持调用自签名SSL证书服务器吗?
顺便说说.哪个客户端默认使用Retrofit?我认为这是OkHttp,但当我研究了一点时,我意识到我需要导入OkHttp依赖项