From 17c0caedd9dac30e296e686d1179e700034f1e30 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 7 Aug 2017 11:18:01 +0800 Subject: [PATCH] fix bug https://github.com/AriaLyy/Aria/issues/88 --- .../com/arialyy/aria/util/CommonUtil.java | 30 ++++++++++++++----- .../java/com/arialyy/simple/MainActivity.java | 1 + 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index e58a3537..b9cf605c 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -47,11 +47,13 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Properties; import java.util.Set; +import java.util.regex.Matcher; import java.util.regex.Pattern; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; @@ -72,24 +74,36 @@ public class CommonUtil { * @return 转换后的地址 */ public static String convertUrl(String url) { - try { - return hasChineseCharacter(url) ? URLEncoder.encode(url, "UTF-8") : url; - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); + if (hasChineseCharacter(url)) { + //匹配双字节字符(包括汉字在内) + String regex = "[^\\x00-\\xff]"; + Pattern p = Pattern.compile(regex); + Matcher m = p.matcher(url); + Set strs = new HashSet<>(); + while (m.find()) { + strs.add(m.group()); + } + try { + for (String str : strs) { + url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8")); + } + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } } - return ""; + return url; } /** - * 判断是否有中文 + * 判断是否有双字节字符(包括汉字在内) * * @param chineseStr 需要进行判断的字符串 - * @return {@code true}有中文,{@code false} 无中文 + * @return {@code true}有双字节字符,{@code false} 无双字节字符 */ public static boolean hasChineseCharacter(String chineseStr) { char[] charArray = chineseStr.toCharArray(); for (char aCharArray : charArray) { - if ((aCharArray >= 0x4e00) && (aCharArray <= 0x9fbb)) { + if (aCharArray <= 0xff) { return true; } } diff --git a/app/src/main/java/com/arialyy/simple/MainActivity.java b/app/src/main/java/com/arialyy/simple/MainActivity.java index 8a8a7cd6..dc0012a7 100644 --- a/app/src/main/java/com/arialyy/simple/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/MainActivity.java @@ -70,4 +70,5 @@ public class MainActivity extends BaseActivity { break; } } + }