android中的HttpUrlConnection的使用之四

來源:互聯網
上載者:User

標籤:

 前面的使用之三我簡單的說了下app與本地的伺服器相串連並進行簡單的資料轉送,但是後來簡單的測試了一下傳輸中文資料,會發現亂碼了!這是怎麼回事呢?這裡我們解釋一下出現這個情況的原因。

1.首先分析原因:這是因為當我們提交資料時,資料編碼格式是iso-8859-1,而伺服器那邊資料的編碼格式是utf-8格式。因此我們想不會出現這種情況的話,必須進行資料轉碼

2.資料轉碼:伺服器後台收到資料我們應該使用相應的方法,將原來的資料格式從iso-8859-1格式轉化為utf-8.

Java代碼(這個是servlet的Java代碼 是伺服器的代碼,不是用戶端)
 1 package com.Servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 11 /**12  * Servlet implementation class Servlet13  */14 public class Servlet extends HttpServlet {15     private static final long serialVersionUID = 1L;16        17     /**18      * @see HttpServlet#HttpServlet()19      */20     public Servlet() {21         super();22         // TODO Auto-generated constructor stub23     }24 25     /**26      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)27      */28     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {29         //調用下面的doPost方法30         this.doPost(request, response);31     }32 33     /**34      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)35      */36     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {37         //從request中分別獲得name age屬性38         String name = request.getParameter("name");39         String age = request.getParameter("age");40         response.setContentType("text/html;charset=utf-8");41         PrintWriter pw = response.getWriter();42         //再網頁上顯示相應的資訊43         System.out.println(new String (name.getBytes("iso-8859-1"), "UTF-8"));44         pw.println("name = " + new String (name.getBytes("iso-8859-1"), "UTF-8") + " age = " + age);45         //在控制上顯示相應的資訊46         System.out.println("11name = " + new String(name.getBytes("iso-8859-1"), "utf-8"));47         System.out.println("age = " + age);48     }49 50 }

將代碼改成這樣過後,我們再去測試,如果發現在網頁顯示的是正確的,而控制台上顯示的卻是亂碼。這個是為什麼呢?

那是因為在編碼的時候, jsp檔案按照的是iso-8859-1的格式編碼,因此要改變它的編碼格式。只需將下面的紅色部分改變了就是了。

jsp代碼

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body>10 11     <form action="Servlet" method = "get">12     name:<input type = "text" name = "name"><br>13     age:<input type = "text" name = "age"><br>14     submit:<input type = "submit" value = "submit"><br>15     </form>16 </body>17 </html>

 

android中的HttpUrlConnection的使用之四

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.