【问题标题】:OkHttp 504 Gatweway time out error in androidandroid中的OkHttp 504网关超时错误
【发布时间】:2018-09-13 14:14:33
【问题描述】:
Response{protocol=http/1.1, code=504, message=GATEWAY_TIMEOUT, url=https://************************}

我来了

code=504, message=GATEWAY_TIMEOUT

在 android 中但相同的 url 在 iOS 中获得了成功

OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(30*1000, TimeUnit.MILLISECONDS)
            .readTimeout(30*1000, TimeUnit.MILLISECONDS)
            .writeTimeout(30*1000, TimeUnit.MILLISECONDS)
            .retryOnConnectionFailure(false)
            .build();

    Request request = new Request.Builder().url(urlStr).post(formBody)
            .addHeader("Authorization", g.getTokenType() + " " + g.getAccessToken())
            .addHeader("Content-type", "application/x-www-form-urlencoded")

    try {
        Response mResponse = client.newCall(request).execute();
        String jsonString = mResponse.body().string();

【问题讨论】:

  • 检查您的服务器,请您再试一次。
  • @HimeshGoswami 在 iOS 中运行良好
  • 您使用的是模拟器还是物理设备?
  • @MohammadTabbara 我正在使用物理设备
  • @shiva 你要尝试在这个 api 中发送图像吗?

标签: android post web service okhttp


【解决方案1】:

试试这个

OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(30*1000, TimeUnit.SECONDS)
        .readTimeout(30*1000, TimeUnit.SECONDS)
        .writeTimeout(30*1000, TimeUnit.SECONDS)
        .retryOnConnectionFailure(false)
        .build();

【讨论】:

  • 这是我的回复 - Response{protocol=http/1.1, code=504, message=GATEWAY_TIMEOUT, url=web-***.*******.com/** *************************************}
【解决方案2】:

根据您的服务器速度时间要求以分钟为单位设置超时。

试试这个:-

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.readTimeout(2, TimeUnit.MINUTES);
        httpClient.connectTimeout(2, TimeUnit.MINUTES);
        httpClient.writeTimeout(2, TimeUnit.MINUTES);

        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();

                Request.Builder builder = original.newBuilder();
                builder.method(original.method(), original.body());
                builder.header("Accept", "application/json");
                if (TOKEN.length() > 0)
                    builder.header("Authorization", TOKEN);
                return chain.proceed(builder.build());
            }
        });

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient.addInterceptor(interceptor);

        OkHttpClient client = httpClient.build();

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

【讨论】:

  • 然后检查您输入的网址是否 100% 正确?比如 http 或 https、域名等。
  • 如果我在我的代码中删除了 retryOnConnectionFailure(false) 它工作正常......但我在我的服务器上收到重复命中。所以我添加 retryOnConnectionFailure(false)
猜你喜欢
  • 2013-05-12
  • 2011-04-08
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 2016-06-19
  • 2015-10-20
相关资源
最近更新 更多