標籤:android http io os 使用 java sp 資料 on
============問題描述============
源碼如下所示,這時候“張三”這個字元到web已經是兩個“??”,怎麼破,查了不少方法,
如URLDecoder.decode(“張三”, "utf-8"),或者"張三".getBytes()都不好用啊,求破
public static String GetXml() throws Exception {
URL postUrl = new URL(“http://10.0.2.2:1234/Android/ANewsManager.aspx?do=add&name=張三”);
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "text/xml");
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "utf-8"));// 設定編碼,否則中文亂碼
String line = "";
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
connection.disconnect();
return sb.toString();
}
============解決方案1============
那你的web那邊是不是也是使用的utf-8編碼呢
============解決方案2============
StringWriter writer = new StringWriter();IOUtils.copy(conn.getInputStream(), writer,"UTF-8");
你這個方法試試
============解決方案3============
直接用瀏覽器提交你這個地址 http://10.0.2.2:1234/Android/ANewsManager.aspx?do=add&name=張三
你就能在地址欄看到是編碼成什麼樣子了,也好測試返回的正常與否
============解決方案4============
把UTF-8改成GBK試試
android向web提交資料,中文亂碼