The use of HttpURLConnection in Android is four

Source: Internet
Author: User

The use of the previous three I simply said the next app with the local server connected and simple data transmission, but then a simple test of the transmission of Chinese data, will find garbled! What's going on here? Here we explain why this is happening.

1. First analyze the reason: this is because when we submit data, the data encoding format is iso-8859-1, and the server over the data encoding format is utf-8 format. So we don't think this is going to happen, we have to transcode the data.

2. Data transcoding: Server backend received data we should use the corresponding method, the original data format from the iso-8859-1 format into Utf-8.

Java code (This is the Java code of the servlet is the server's code, not the client)
1  Packagecom. Servlet;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten  One /** A * Servlet Implementation class servlet -  */ -  Public classServletextendsHttpServlet { the     Private Static Final LongSerialversionuid = 1L; -         -     /** -      * @seeHttpservlet#httpservlet () +      */ -      PublicServlet () { +         Super(); A         //TODO auto-generated Constructor stub at     } -  -     /** -      * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response) -      */ -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { in         //call the following Dopost method -          This. DoPost (request, response); to     } +  -     /** the      * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response) *      */ $     protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {Panax Notoginseng         //get the name age property separately from request -String name = Request.getparameter ("name"); theString age = request.getparameter (' Age '); +Response.setcontenttype ("Text/html;charset=utf-8"); APrintWriter PW =Response.getwriter (); the         //and display the corresponding information on the page . +System.out.println (NewString (Name.getbytes ("iso-8859-1"), "UTF-8")); -Pw.println ("name =" +NewString (Name.getbytes ("iso-8859-1"), "UTF-8") + "age =" +Age ); $         //display the appropriate information on the control $System.out.println ("11name =" +NewString (Name.getbytes ("iso-8859-1"), "Utf-8")); -System.out.println ("Age =" +Age ); -     } the  -}

After we have changed the code so that we can test again, if we find that the Web page is displayed correctly, the console displays garbled characters. What is this for?

That's because when coding, the JSP file is encoded in iso-8859-1 format, so change its encoding format. Just change the red part below.

JSP code

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 UTF-8"> 7 <title>insert title here</title> 8 
    </form>16 </body>17 

The use of HttpURLConnection in Android is four

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.