1. 中文亂碼
final HttpClient client = AndroidHttpClient.newInstance("Android");
final HttpGet getRequest = new HttpGet(url.trim());
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
if (HttpStatus.SC_SEE_OTHER == statusCode) { //303跳轉,從location字段中獲取返回的跳轉地址url
String location = response.getLastHeader("Location").getValue();
...
}
}
...
} catch (...) {
...
}
...
解決方法:先將亂碼編碼成 iso8859-1,然後再解碼成 utf-8 即可
String location = response.getLastHeader("Location").getValue();
int nameIndex = location.lastIndexOf("/") + 1;
String name = new String(location.substring(nameIndex).getBytes("iso8859-1"), "utf-8"); //解碼、編碼
String locPre = location.substring(0, nameIndex);
location = locPre + name;
2. 中文編碼成UTF-8
int nameIndex = url.lastIndexOf("/") + 1;
String name = url.substring(nameIndex);
String urlPre = url.substring(0, nameIndex);
url = urlPre + URLEncoder.encode(name, "utf-8");