From 4eb0edaab6236220491e1227ef670a97e107f6da Mon Sep 17 00:00:00 2001 From: panqihua <1029559041@qq.com> Date: Tue, 14 Apr 2020 11:36:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 +- .../pocket/ui/login/LoginActivity.java | 2 ++ .../com/community/pocket/util/HttpUtil.java | 36 +++++++++++++++++++ .../com/community/pocket/util/InitApp.java | 1 + app/src/main/res/values-en-rUS/strings.xml | 2 ++ app/src/main/res/values-zh-rCN/strings.xml | 2 ++ app/src/main/res/values/strings.xml | 2 ++ 7 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b58dbcb..8c45335 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,7 +10,7 @@ android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/AppTheme.NoActionBar"> + android:theme="@style/Theme.AppCompat.Light.NoActionBar"> diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java index 33a2e24..0f37a7a 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java @@ -107,6 +107,7 @@ public class LoginActivity extends BaseActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + HttpUtil.checkServer(this); loginViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(LoginViewModel.class); @@ -195,6 +196,7 @@ public class LoginActivity extends BaseActivity { public void onParseOk(@NotNull Call call, @NotNull okhttp3.Response response, LoginResponse loginResponse) { loginViewModel.getCheckToken().postValue(loginResponse); } + }), new FormBody.Builder() .add("token", currentToken.getToken()) diff --git a/app/src/main/java/com/community/pocket/util/HttpUtil.java b/app/src/main/java/com/community/pocket/util/HttpUtil.java index dbf93f7..c64e837 100644 --- a/app/src/main/java/com/community/pocket/util/HttpUtil.java +++ b/app/src/main/java/com/community/pocket/util/HttpUtil.java @@ -1,9 +1,17 @@ package com.community.pocket.util; +import android.content.Context; +import android.os.Looper; + import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import com.community.pocket.BuildConfig; +import com.community.pocket.R; + +import org.jetbrains.annotations.NotNull; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -14,6 +22,7 @@ import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; +import okhttp3.Response; public class HttpUtil { @@ -43,6 +52,33 @@ public class HttpUtil { call.enqueue(callback); } + //检查服务端连接 + public static void checkServer(final Context context) { + //1.创建OkHttpClient对象 + OkHttpClient okHttpClient = new OkHttpClient(); + //2.创建Request对象,设置一个url地址(百度地址),设置请求方式。 + Request.Builder builder = new Request.Builder().url(BuildConfig.API_HOST); + //3.创建一个call对象,参数就是Request请求对象 + Call call = okHttpClient.newCall(builder.build()); + //4.请求加入调度,重写回调方法 + call.enqueue(new Callback() { + @Override + public void onFailure(@NotNull Call call, @NotNull IOException e) { + if (BuildConfig.DEBUG) { + Looper.prepare(); + new AlertDialog.Builder(context) + .setMessage(context.getString(R.string.check_server_fail, BuildConfig.API_HOST)).show(); + Looper.loop(); + } + } + + @Override + public void onResponse(@NotNull Call call, @NotNull Response response) { + + } + }); + } + public enum Method { GET, POST diff --git a/app/src/main/java/com/community/pocket/util/InitApp.java b/app/src/main/java/com/community/pocket/util/InitApp.java index 7933004..f90c926 100644 --- a/app/src/main/java/com/community/pocket/util/InitApp.java +++ b/app/src/main/java/com/community/pocket/util/InitApp.java @@ -47,4 +47,5 @@ public class InitApp extends Application { LocalManageUtil.saveSystemCurrentLanguage(getApplicationContext(), newConfig); MultiLanguage.onConfigurationChanged(getApplicationContext()); } + } diff --git a/app/src/main/res/values-en-rUS/strings.xml b/app/src/main/res/values-en-rUS/strings.xml index dabb2a8..301d648 100644 --- a/app/src/main/res/values-en-rUS/strings.xml +++ b/app/src/main/res/values-en-rUS/strings.xml @@ -175,4 +175,6 @@ check ok check fail uncheck + connect server ok + connect server:%1s fail \ No newline at end of file diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index e8c5834..abbe476 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -175,4 +175,6 @@ 审核通过 审核不通过 未审核 + 连接服务端成功 + 调试信息:无法连接到服务端:%1s \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1af2f5e..4bb129e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -176,6 +176,8 @@ check ok check fail uncheck + connect server ok + connect server:%1s fail