应用程序在playstore上发布时java OkHttp连接超时
我有一个已经在playstore上发布的应用程序,在这个发布的应用程序中,Okhttp连接保持返回超时,不像我从安卓 studio在安卓设备上调试我的应用程序那样
这是我的连接脚本
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);
}
}
这是我的身材。格拉德尔
implementation 'com.google.安卓.material:material:1.1.0'
implementation '安卓x.legacy:legacy-support-v4:1.0.0'
implementation 'com.balysv:material-ripple:1.0.2'
implementation '安卓x.constraintlayout:constraintlayout:1.1.3'
implementation '安卓x.appcompat:appcompat:1.1.0'
implementation '安卓x.cardview:cardview:1.0.0'
implementation '安卓x.recyclerview:recyclerview:1.1.0'
implementation 'com.安卓.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'
这是我的ActivitySplash。爪哇
@Override
protected void onResume() {
super.onResume();
// permission checker for 安卓 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);
}
有人能解释一下我做错了什么吗,因为我是java 安卓新手。谢谢
# 1 楼答案
碰巧graddle中的minifyenable=false设置对我有用