android向web提交資料,中文亂碼

來源:互聯網
上載者:User

標籤: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提交資料,中文亂碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.