From c5b58c41e15da2ad124c7dfa51dd80fef77cfe77 Mon Sep 17 00:00:00 2001 From: ZHD <123456> Date: Fri, 1 May 2020 18:44:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A9=BA=E5=A4=B4=E5=83=8F?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 5 +- .../pocket/data/main/info/InfoRequest.java | 3 + .../com/community/pocket/util/HttpUtil.java | 104 +++++++++--------- app/src/main/res/values-zh-rCN/strings.xml | 2 +- .../main/res/xml/network_security_config.xml | 4 + 5 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 app/src/main/res/xml/network_security_config.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5005786..a7bfbdf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -14,7 +15,9 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar" - android:largeHeap="true"> + android:largeHeap="true" + android:networkSecurityConfig="@xml/network_security_config" + tools:targetApi="n"> diff --git a/app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java b/app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java index 7b00681..0b61d47 100644 --- a/app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java @@ -63,6 +63,9 @@ public class InfoRequest { //获取头像 public void getImg(MutableLiveData liveData, String url) { + if(url==null){ + return; + } HttpUtil.getRequest(new HttpFileResponse(liveData), url); } } 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 8d78fe2..c2c096f 100644 --- a/app/src/main/java/com/community/pocket/util/HttpUtil.java +++ b/app/src/main/java/com/community/pocket/util/HttpUtil.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Looper; +import android.util.Log; import android.util.Patterns; import android.util.TypedValue; import android.view.View; @@ -129,56 +130,61 @@ public class HttpUtil { @Override public void onFailure(@NotNull Call call, @NotNull IOException e) { bool.postValue(false); - if (BuildConfig.DEBUG) { - Looper.prepare(); - AlertDialog.Builder builder = getBuilder(context); - ConstraintLayout constraintLayout = (ConstraintLayout) View.inflate(context, R.layout.debug, null); - TextView buildType = constraintLayout.findViewById(R.id.build_type); - buildType.setText(context.getString(R.string.build_type, BuildConfig.BUILD_TYPE)); - TextView url = constraintLayout.findViewById(R.id.url); - url.setText(context.getString(R.string.server_host, BuildConfig.API_HOST)); - TextView detailError = constraintLayout.findViewById(R.id.detailError); - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - e.printStackTrace(printWriter); - detailError.setText(stringWriter.toString()); - Button openUrl = constraintLayout.findViewById(R.id.open_url); - TextView error = constraintLayout.findViewById(R.id.error); - LinearLayout linearLayout = constraintLayout.findViewById(R.id.my_layout); - int index = linearLayout.indexOfChild(error); - - TextView testPort = new TextView(context); - TextView mode = constraintLayout.findViewById(R.id.mode); - switch (BuildType.valueOf(BuildConfig.BUILD_TYPE)) { - case rap2debug: - testPort.setText(R.string.rap2_error); - mode.setText(context.getString(R.string.debug_mode, BuildType.rap2debug.getName())); - break; - case serverdebug: - testPort.setText(R.string.server_error); - mode.setText(context.getString(R.string.debug_mode, BuildType.serverdebug.getName())); - break; - default: + Log.e(HttpUtil.class.getName(),e.toString()); + try { + if (BuildConfig.DEBUG) { + Looper.prepare(); + AlertDialog.Builder builder = getBuilder(context); + ConstraintLayout constraintLayout = (ConstraintLayout) View.inflate(context, R.layout.debug, null); + TextView buildType = constraintLayout.findViewById(R.id.build_type); + buildType.setText(context.getString(R.string.build_type, BuildConfig.BUILD_TYPE)); + TextView url = constraintLayout.findViewById(R.id.url); + url.setText(context.getString(R.string.server_host, BuildConfig.API_HOST)); + TextView detailError = constraintLayout.findViewById(R.id.detailError); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + e.printStackTrace(printWriter); + detailError.setText(stringWriter.toString()); + Button openUrl = constraintLayout.findViewById(R.id.open_url); + TextView error = constraintLayout.findViewById(R.id.error); + LinearLayout linearLayout = constraintLayout.findViewById(R.id.my_layout); + int index = linearLayout.indexOfChild(error); + + TextView testPort = new TextView(context); + TextView mode = constraintLayout.findViewById(R.id.mode); + switch (BuildType.valueOf(BuildConfig.BUILD_TYPE)) { + case rap2debug: + testPort.setText(R.string.rap2_error); + mode.setText(context.getString(R.string.debug_mode, BuildType.rap2debug.getName())); + break; + case serverdebug: + testPort.setText(R.string.server_error); + mode.setText(context.getString(R.string.debug_mode, BuildType.serverdebug.getName())); + break; + default: + } + testPort.setSingleLine(false); + testPort.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); + testPort.setGravity(error.getGravity()); + linearLayout.addView(testPort, index + 1); + + openUrl.setOnClickListener(v -> { + //从其他浏览器打开 + Intent intent = new Intent(); + intent.setAction(Intent.ACTION_VIEW); + Uri contentUrl = Uri.parse(BuildConfig.API_HOST); + intent.setData(contentUrl); + Activity activity = (Activity) context; + activity.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_browser))); + + }); + + builder.setView(constraintLayout); + builder.show(); + Looper.loop(); } - testPort.setSingleLine(false); - testPort.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); - testPort.setGravity(error.getGravity()); - linearLayout.addView(testPort, index + 1); - - openUrl.setOnClickListener(v -> { - //从其他浏览器打开 - Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - Uri contentUrl = Uri.parse(BuildConfig.API_HOST); - intent.setData(contentUrl); - Activity activity = (Activity) context; - activity.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_browser))); - - }); - - builder.setView(constraintLayout); - builder.show(); - Looper.loop(); + } catch (Exception ex) { + Log.e(HttpUtil.class.getName(),ex.toString()); } } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index abe1d5d..f003d36 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -207,7 +207,7 @@ 尝试使用浏览器打开服务端 请选择浏览器 请按照以下步骤检查:\n1.rap2服务端ip地址是否正确\n2.rap2服务是否已启动\n3.浏览器是否能访问rap2的web服务(默认:3000端口) - 请按照以下步骤检查:\n1.springboot服务端ip地址是否正确\n2.mongo数据库服务是否已启动\n3.springboot服务端是否已启动 + 请按照以下步骤检查:\n1.springboot服务端ip地址是否正确\n2.mongo数据库服务是否已启动\n3.springboot服务端是否已启动\n4.如果是真机调试,请确认手机IP和服务端IP处于同一个网段 %1s联调模式 是否退出应用? diff --git a/app/src/main/res/xml/network_security_config.xml b/app/src/main/res/xml/network_security_config.xml new file mode 100644 index 0000000..83a6d61 --- /dev/null +++ b/app/src/main/res/xml/network_security_config.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file