标签: okhttp

如何防止Retrofit自动跟随302

我有一个身份验证调用,我正在尝试使用Android上的Retrofit.该调用将302返回到成功或失败页面.最初的302响应会返回维护身份验证成功所需的会话cookie,但是在我有机会使用cookie之前,Retrofit会自动将请求关闭到重定向URL.

有没有办法阻止重定向?或者有没有办法在Retrofit上编写响应处理程序,可以在进行第二次调用之前添加适当的标头?

cookies android retrofit okhttp

17
推荐指数
3
解决办法
9991
查看次数

从资源上传okhttp的二进制文件

我需要使用okhttp将apk中捆绑的二进制文件上传到服务器.使用urlconnection,您只需获取资产的输入流,然后将其放入您的请求中.但是,okhttp只提供上传字节数组,字符串或文件的选项.由于您无法获取apk中捆绑的资产的文件路径,因此将文件复制到本地文件目录的唯一选项(我宁愿不这样做)然后将文件提供给okhttp?有没有办法简单地使用assetinputstream直接向Web服务器发出请求?

编辑:我使用了接受的答案,但我没有创建一个静态实用程序类,而只是将子类化为RequestBody

 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 = null; …
Run Code Online (Sandbox Code Playgroud)

android okhttp

17
推荐指数
1
解决办法
1万
查看次数

既然在Android上不推荐使用SSLSocketFactory,那么处理客户端证书身份验证的最佳方法是什么?

我正在开发一款需要客户端证书身份验证(包含PKCS 12文件)的Android应用.随着所有这些的弃用apache.http.*,我们已经开始在我们的网络层上进行重构,我们决定使用OkHttp作为替代品,到目前为止我非常喜欢它.

但是,我没有找到任何其他方法来处理客户端证书auth而不使用SSLSocketFactoryOkHttp或其他任何事情.那么在这种特殊情况下,最好的做法是什么?OkHttp有另一种方法来处理这种身份验证吗?

ssl android pkcs#12 sslsocketfactory okhttp

17
推荐指数
2
解决办法
2万
查看次数

使用Retrofit 2.0.x进行HTTP缓存

我正在尝试使用Retrofit 2.0在我的应用程序中缓存一些响应,但我遗漏了一些东西.

我安装了一个缓存文件,如下所示:

private static File httpCacheDir;
private static Cache cache;
try {
    httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
    httpCacheDir.setReadable(true);
    long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
    HttpResponseCache.install(httpCacheDir, httpCacheSize);
    cache = new Cache(httpCacheDir, httpCacheSize);
    Log.i("HTTP Caching", "HTTP response cache installation success");
} catch (IOException e) {
    Log.i("HTTP Caching", "HTTP response cache installation failed:" + e);
}

public static Cache getCache() {
        return cache;
    }
Run Code Online (Sandbox Code Playgroud)

在其中创建一个文件 /data/user/0/<PackageNmae>/cache/http ,然后准备一个网络拦截器如下:

public class CachingControlInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain …
Run Code Online (Sandbox Code Playgroud)

android caching retrofit okhttp

17
推荐指数
2
解决办法
1万
查看次数

OKhttp PUT示例

我的要求是使用PUT,发送标题和正文到服务器,它将更新数据库中的内容.

我刚刚阅读了okHttp文档,我试图使用他们的POST示例,但它不适用于我的用例(我认为这可能是因为服务器要求我使用PUT而不是POST).

这是我的方法POST:

 public void postRequestWithHeaderAndBody(String url, String header, String jsonBody) {


        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, jsonBody);

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Authorization", header)
                .build();

        makeCall(client, request);
    }
Run Code Online (Sandbox Code Playgroud)

我试图搜索okHttp示例使用PUT没有成功,如果我需要使用PUT方法是否还有使用okHttp?

我正在使用okhttp:2.4.0(以防万一),感谢任何帮助!

android android-networking androidhttpclient retrofit okhttp

17
推荐指数
2
解决办法
1万
查看次数

如何使用Retrofit 2.0获得原始响应和请求

我试图使用Retrofit2.0.2获得原始响应.

到目前为止,我尝试使用以下代码行打印响应,但它打印的地址而不是确切的响应正文.

Log.i("RAW MESSAGE",response.body().toString());

compile 'com.squareup.retrofit2:retrofit:2.0.2'

    Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();


            GitApi gitApi = retrofit.create(GitApi.class);

            Call<Addresses> call = gitApi.getFeed(user);

    call.enqueue(new Callback<Addresses>() {

                @Override
                public void onResponse(Response<Addresses> response, Retrofit retrofit) {
                    try {
                        mDisplayDetails.setText(response.body().getSuburbs().get(0).getText());

                    **Log.i("RAW MESSAGE",response.body().toString());**

                    } catch (Exception e) {
                        mDisplayDetails.setText(e.getMessage());
                    }
                    mProgressBar.setVisibility(View.INVISIBLE);

                }

                @Override
                public void onFailure(Throwable t) {
                    mDisplayDetails.setText(t.getMessage());
                    mProgressBar.setVisibility(View.INVISIBLE);

                }
            });
Run Code Online (Sandbox Code Playgroud)

java android retrofit okhttp retrofit2

17
推荐指数
2
解决办法
1万
查看次数

android - OkHttp拦截器 - 响应已经"消耗"

我正在尝试使用此拦截器进行身份验证:

public class CustomInterceptor implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    // try the request
    Response response = chain.proceed(request);

    if (response shows expired token) {

        // get a new token (I use a synchronous Retrofit call)

        // create a new request and modify it accordingly using the new token
        Request newRequest = request.newBuilder()...build();

        // retry the request
        return chain.proceed(newRequest);
    }

    // otherwise just pass the original response on
    return response;
}
Run Code Online (Sandbox Code Playgroud)

问题是我的检查(响应显示过期令牌)与状态无关,我需要检查实际响应(正文内容).因此,在检查之后,响应被"消耗"并且任何准备身体的尝试都将失败. …

android response interceptor okhttp

16
推荐指数
1
解决办法
3573
查看次数

javax.net.ssl.SSLPeerUnverifiedException:未验证主机名:

我正在尝试使用HTTPS与自签名证书的连接.
我按照这里提到的创建自签名证书的步骤 - 创建自签名证书.
即使在浏览器中一切正常,它只向我显示我的证书由未知CA签名的消息.
但是我的证书中的FQDN(服务器名称不匹配)名称有问题,因为我在生成证书时设置了错误的名称.
我已经重新生成它,现在没有这样的错误.

我需要从移动Android客户端使用我的服务器sertificate,我找到了关于此问题的精彩文章 - 在Android中使用自签名或未知SSL证书的Retrofit.我已经遵循了所有步骤,但遗憾的是得到了一个错误(例外).

javax.net.ssl.SSLPeerUnverifiedException: Hostname 195.xx.xx.xx not verified:
    certificate: sha1/qvH7lFeijE/ZXxNHI0B/M+AU/aA=
    DN: 1.2.840.113549.1.9.1=#160e63726f73704078616b65702e7275,CN=195.xx.xx.xx,OU=Departament of Development,O=CROSP Solutions,L=Chernihiv,ST=Chernihiv,C=UA
    subjectAltNames: []
            at com.squareup.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:124)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的主机名相同,但仍然存在错误.
请帮忙处理这个问题,我将不胜感激任何帮助.
谢谢.

伪解决方案

当然我之前搜索过并找到了HostName Verifier Solution.
我试过了,它有效.但是可以使用这种解决方法,我将证书添加到我的应用程序中,以便像在前面的示例中一样动态地读取它,它是否仍然在这种情况下使用.

OkHttp的解决方案是一行.(如果您按照教程中的所有步骤操作).

 okHttpClient.setHostnameVerifier(new NullHostNameVerifier());
Run Code Online (Sandbox Code Playgroud)

但我还是觉得这不是最好的解决方案,请问有什么想法吗?

java ssl https android okhttp

16
推荐指数
1
解决办法
2万
查看次数

Android OkHttp,刷新过期令牌

场景:我使用OkHttp/Retrofit访问Web服务:同时发送多个HTTP请求.在某些时候,身份验证令牌到期,并且多个请求将获得401响应.

问题:在我的第一个实现中,我使用拦截器(这里简化),每个线程都尝试刷新令牌.这导致一团糟.

public class SignedRequestInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        // 1. sign this request
        request = request.newBuilder()
                    .header(AUTH_HEADER_KEY, BEARER_HEADER_VALUE + token)
                    .build();


        // 2. proceed with the request
        Response response = chain.proceed(request);

        // 3. check the response: have we got a 401?
        if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {

            // ... try to refresh the token
            newToken = mAuthService.refreshAccessToken(..);


            // sign the request with the new token and proceed …
Run Code Online (Sandbox Code Playgroud)

android retrofit okhttp

16
推荐指数
3
解决办法
6866
查看次数

okhttp中的java.net.SocketTimeoutException

这需要花费很多时间来获取数据json.当删除并再次安装它将json在1分钟内得到,当我再次点击按钮,json它需要这么多时间,仍然数据没有进入listview

这是我的异常代码

E/JSONDemo: IOExceptiojava.net.SocketTimeoutException
  at java.net.PlainSocketImpl.read(PlainSocketImpl.java:493)
  at java.net.PlainSocketImpl.-wrap0(PlainSocketImpl.java)
  at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:242)
  at okio.Okio$2.read(Okio.java:140)
  at okio.AsyncTimeout$2.read(AsyncTimeout.java:238)
  at okio.RealBufferedSource.indexOf(RealBufferedSource.java:325)
  at okio.RealBufferedSource.indexOf(RealBufferedSource.java:314)
  at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210)
  at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
  at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
  at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:775)
  at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:86)
  at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:760)
  at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:613)
  at okhttp3.RealCall.getResponse(RealCall.java:244)
  at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
  at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
  at okhttp3.RealCall.access$100(RealCall.java:30)
  at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
  at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
  at java.lang.Thread.run(Thread.java:818)
Run Code Online (Sandbox Code Playgroud)

这是java文件中的json代码:

progress = ProgressDialog.show(MainActivity.this, "dialog title", "dialog message", true);
Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_LONG).show();

    if (isNetworkAvailable()) {

        String url = "ConstantValue.URL";
        RequestBody formBody = new …
Run Code Online (Sandbox Code Playgroud)

android json socketexception okhttp okhttp3

16
推荐指数
2
解决办法
2万
查看次数