Android: the client obtains garbled data from the server.

Source: Internet
Author: User

Android: the client obtains garbled data from the server.
Send an HTTP request to the server. The received JSON package is response. Use String content = EntityUtils. toString (response. getEntity (), "UTF-8"); Chinese garbled characters are still involved in decoding, followed

        String name = new String(response.getBytes("iso-8859-1"), "UTF-8");  

It does not help. Think of the server as if it was coded with URLENCODER, with a try attitude behind return added a URLDecoder. decode (content, "UTF-8"); it really works! However, I still don't quite understand the differences between URLDecoder. decode (content, "UTF-8") and EntityUtils. toString (response. getEntity (), "UTF-8") during decoding. The following is the network code:

 

Package com. trilink. ibeaconlocationdisplay. utils; import java. io. unsupportedEncodingException; import java. util. list; import org. apache. http. httpResponse; import org. apache. http. httpStatus; import org. apache. http. nameValuePair; import org. apache. http. client. httpClient; import org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpPost; import org. apache. http. impl. clien T. defaultHttpClient; import org. apache. http. params. basicHttpParams; import org. apache. http. params. httpConnectionParams; import org. apache. http. util. entityUtils; import android. util. log; public class NetworkService {private static String TAG = "NetworkService"; // private static String url_ip = ServerUrl. SERVER_ADRESS + "UserInfoServlet? "; // Private static String url_ip =" http: // 192.168.1.231: 8080/indoor/";/*** release resource */public static void cancel () {Log. I (TAG, "cancel! "); // If (conn! = Null) {// conn. cancel (); // public static String getPostResult (String url) that is passed without parameters {// create an http request object HttpPost post = new HttpPost (url ); // create HttpParams to set the HTTP parameter BasicHttpParams httpParams = new BasicHttpParams (); HttpConnectionParams. setConnectionTimeout (httpParams, 10*1000); HttpConnectionParams. setSoTimeout (httpParams, 10*1000); // create a network access processing object HttpClient httpClient = new DefaultHttpClient (httpParams); t Ry {// execute the request parameter HttpResponse response = httpClient.exe cute (post); // determine whether the request is successful if (response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// obtain the response String content = EntityUtils. toString (response. getEntity (); return URLDecoder. decode (content, "UTF-8") ;}} catch (Exception e) {e. printStackTrace (); return "{\" status \ ": 405, \" resultMsg \ ": \" Network timeout! \ "}" ;}Finally {// release the network connection resource httpClient. getConnectionManager (). shutdown ();} return "{\" status \ ": 405, \" resultMsg \ ": \" Network timeout! \ "}" ;}// Public static String getPostResult (String url, List
 
  
ParamList) {UrlEncodedFormEntity entity = null; try {entity = new UrlEncodedFormEntity (paramList, "UTF-8");} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace ();} // create an http request object HttpPost post = new HttpPost (url); BasicHttpParams httpParams = new BasicHttpParams (); HttpConnectionParams. setConnectionTimeout (httpParams, 10*1000); HttpConnectionParams. setSoTimeout (ht TpParams, 10*1000); post. setEntity (entity); // create the network access processing object HttpClient httpClient = new DefaultHttpClient (httpParams); try {// execute the request parameter HttpResponse response = httpClient.exe cute (post ); // determine whether the request is successful if (response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// obtain the response String content = EntityUtils. toString (response. getEntity (), "UTF-8"); return URLDecoder. decode (content, "UTF-8") ;}} catch (Exception e) {e. print StackTrace (); return "{\" status \ ": 405, \" resultMsg \ ": \" Network timeout! \ "}" ;}Finally {// release the network connection resource httpClient. getConnectionManager (). shutdown ();} return "{\" status \ ": 405, \" resultMsg \ ": \" Network timeout! \"}";}}
 


 

Related Article

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.