在使用 okhttp 的時候,head 的一些項是中文,導致網絡請求失敗,錯誤類似下面的
java.lang.IllegalArgumentException: Unexpected char ...
Header values are (technically) required to be ISO-8859-1 but in practice only ASCII really works and OkHttp validates such (the exception has nothing to do with Retrofit). That string is a custom user agent and you'll need to restrict its contents to the ASCII character set when creating it.
具體的代碼 ( 在 okhttp3 庫裏面的 okhttp3.Headers.java ) 如下:
private void checkNameAndValue(String name, String value) {
if (name == null) throw new NullPointerException("name == null");
if (name.isEmpty()) throw new IllegalArgumentException("name is empty");
for (int i = 0, length = name.length(); i < length; i++) {
char c = name.charAt(i);
if (c <= '\u001f' || c >= '\u007f') {
throw new IllegalArgumentException(Util.format(
"Unexpected char %#04x at %d in header name: %s", (int) c, i, name));
}
}
if (value == null) throw new NullPointerException("value == null");
for (int i = 0, length = value.length(); i < length; i++) {
char c = value.charAt(i);
if (c <= '\u001f' || c >= '\u007f') {
throw new IllegalArgumentException(Util.format(
"Unexpected char %#04x at %d in %s value: %s", (int) c, i, name, value));
}
}
}
看源碼,解決的方法很簡單,保證所有的 head 都是符合編碼要求。
但是我的情況是,其中的某一些字符不合要求,這部分服務器不關心,而本地卻又不知道具體哪些是不和要求的。我的解決思路很簡單,挑出不合要求的字符,把這些字符單獨轉碼。
private static String encodeHeadInfo( String headInfo ) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0, length = headInfo.length(); i < length; i++) {
char c = headInfo.charAt(i);
if (c <= '\u001f' || c >= '\u007f') {
stringBuffer.append( String.format ("\\u%04x", (int)c) );
} else {
stringBuffer.append(c);
}
}
return stringBuffer.toString();
}
這樣