简体   繁体   中英

OkHttp connection timeout when app is published on playstore

I have an app that already published on playstore, in this published app the Okhttp connection keeps return timeout not like when I debug my app on android device from android studio.

Here is my connection script

 public class RestAdapter {

    public static API createAPI() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.connectTimeout(175, TimeUnit.SECONDS);
        builder.writeTimeout(170, TimeUnit.SECONDS);
        builder.readTimeout(190, TimeUnit.SECONDS);

        if(BuildConfig.DEBUG){
            builder.addInterceptor(logging);
        }
        builder.cache(null);
        OkHttpClient okHttpClient = builder.build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.WEB_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        return retrofit.create(API.class);
    }
}

And here is my build.gradle

    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.balysv:material-ripple:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'

    implementation 'com.android.support:multidex:1.0.3'

    // library for api
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.3.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.google.code.gson:gson:2.8.5'

    // glide image loader
    implementation 'com.github.bumptech.glide:glide:3.7.0'

    // Firebase core library
    implementation 'com.google.firebase:firebase-core:17.4.1'
    // Firebase notification library
    implementation 'com.google.firebase:firebase-messaging:20.2.0'

And here is my ActivitySplash.java

@Override
    protected void onResume() {
        super.onResume();

        // permission checker for android M or higher
        if (Tools.needRequestPermission() && !on_permission_result) {
            String[] permission = PermissionUtil.getDeniedPermission(this);
            if (permission.length != 0) {
                requestPermissions(permission, 200);
            } else {
                startProcess();
            }
        } else {
            startProcess();
        }
    }

    private void startProcess() {
        if (!NetworkCheck.isConnect(this)) {
            dialogNoInternet();
        } else {
            requestInfo();
        }
    }
    
   private void requestInfo() {
        API api = RestAdapter.createAPI();

        Log.d("this", "getVersionCode " + Tools.getVersionCode(this));

        Call<CallbackInfo> callbackCall = api.getInfo(Tools.getVersionCode(this));
        callbackCall.enqueue(new Callback<CallbackInfo>() {
            @Override
            public void onResponse(Call<CallbackInfo> call, Response<CallbackInfo> response)
            {
                CallbackInfo resp = response.body();
                Log.d("this", "CallbackInfo resp " + resp);
                if (resp != null && resp.status.equals("success") && resp.info != null)
                {
                    Info info = sharedPref.setInfoData(resp.info);
                    checkAppVersion(info);
                }
                else {

                    dialogServerNotConnect();
                }
            }

            @Override
            public void onFailure(Call<CallbackInfo> call, Throwable t) {
                Log.e("onFailure", t.getMessage());
                dialogServerNotConnect();
            }
        });
    }
    
    public void dialogServerNotConnect()
    {
        Dialog dialog = new DialogUtils(this).buildDialogWarning(R.string.title_unable_connect, R.string.msg_unable_connect, R.string.TRY_AGAIN, R.string.CLOSE, R.drawable.img_no_connect, new CallbackDialog()
        {
            @Override
            public void onPositiveClick(Dialog dialog)
            {
                dialog.dismiss();
                retryOpenApplication();
            }

            @Override
            public void onNegativeClick(Dialog dialog) {
                finish();
            }
        });
        dialog.show();
    }
  // make a delay to start next activity
    private void retryOpenApplication() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startProcess();
            }
        }, 2000);
    }

Can someone explain to me what I'm doing wrong, because I'm new to java android. Thanks

碰巧在 graddle 中设置 minifyenable=false 对我有用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM