綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 為什麼 HttpPost 回傳後的中文字都變成亂碼了?

[複製連結] 查看: 2167|回覆: 7|好評: 0
跳轉到指定樓層
樓主
abcd30282000 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2013-4-18 18:53
30碎鑽
我用 HttpPost 擷取網路回傳的資料

可是中文字的部分都變成亂碼了

我 Google 好幾天

也都照著設定過

可是還是亂碼

要怎麼修改才可以解決問題呢?

謝謝

程式碼如下:


-------------------------------------------------------------------


new Thread() {
   @Override
   public void run() {
      HttpPost httpRequest = new HttpPost("http://140.121.196.8/WS/login.php");

      List<NameValuePair> params = new ArrayList<NameValuePair>();
      params.add(new BasicNameValuePair("account", account.getText().toString()));
      params.add(new BasicNameValuePair("password", password.getText().toString()));
      try {
         httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
         HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);

         if (httpResponse.getStatusLine().getStatusCode() == 200){
            final String result = EntityUtils.toString(httpResponse.getEntity());
            runOnUiThread (new Runnable() {
               public void run() {
                  Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();

               }
            
            });
         
         }
         else
            Toast.makeText(MainActivity.this, "回傳失敗", Toast.LENGTH_LONG).show();
      
      }
      catch(Exception e) {
         Toast.makeText(MainActivity.this, "沒有回應", Toast.LENGTH_LONG).show();

      }

   }

}

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
whl | 收聽TA | 只看該作者
發表於 2013-4-18 19:22
回傳的內容是 utf-8 還是 big5?

點評

是UTF8沒錯  發表於 2013-4-18 20:02
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

板凳
whl | 收聽TA | 只看該作者
發表於 2013-4-18 21:28
試試看 urldecoder
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

地板
 樓主| abcd30282000 | 收聽TA | 只看該作者
發表於 2013-4-18 22:31

我把【final String result = EntityUtils.toString(httpResponse.getEntity());】這一行
改成【final String result = URLEncoder.encode(EntityUtils.toString(httpResponse.getEntity()), "utf-8");】
可是亂碼愈來愈多的說
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

5
whl | 收聽TA | 只看該作者
發表於 2013-4-19 00:32
是 urldecoder 不是 urlencoder

點評

我晚上下班會重新試試,謝謝你^^  發表於 2013-4-19 11:18

評分

參與人數 1碎鑽 +1 幫助 +1 技術 +1 收起 理由
abcd30282000 + 1 + 1 + 1 神馬都是浮雲

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

6
 樓主| abcd30282000 | 收聽TA | 只看該作者
發表於 2013-4-19 17:18
whl 發表於 2013-4-19 00:32
是 urldecoder 不是 urlencoder

不知道是不是把我上面改的程式碼當中的 URLEncoder 直接改成 URLDecoder 就好了?
可是我試了之後
還是跟最一開始的亂碼一模一樣
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

7
whl | 收聽TA | 只看該作者
發表於 2013-4-19 18:17
檢查一下 server 設定, 我不知道是不是 Content-Encoding : gzip 這個問題。
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

8
 樓主| abcd30282000 | 收聽TA | 只看該作者
發表於 2013-4-19 18:29
whl 發表於 2013-4-19 18:17
檢查一下 server 設定, 我不知道是不是 Content-Encoding : gzip 這個問題。

所以你的意思是可能問題是出在網頁
而不是程式碼嗎?
謝謝^__^
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則