Sends an HTTP request to the server. The JSON packet received is response, with string content = Entityutils.tostring (response.getentity (), "Utf-8"), decoding or a Chinese garbled, added in the back
String name = new String (Response.getbytes ("iso-8859-1"), "UTF-8");
is useless.
Think of the server as if it was Urlencoder code. With a try attitude in return after adding a urldecoder.decode (content, "utf-8");
It's just not clear what the difference is between urldecoder.decode (content, "Utf-8") and Entityutils.tostring (Response.getentity (), "Utf-8") when decoding. The following is the network-side 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.client.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.serve r_adress+ "Userinfoservlet?"; /private static String url_ip = "http://192.168.1.231:8080/indoor/";/** * Frees resources */public static void Cancel () {log.i (TAG, " Cancel! "); /IF (conn! = null) {//Conn.cancel ();//}}//No parameters passed public static string Getpostresult (string url) {//Create HTTP Request Object HttpPost POS t = new HTTPPOST (URL);//create Httpparams to set HTTP parameters 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); try{//Run request parameter HttpResponse response = Httpclient.execute (post);//Infer if the request succeeds if (Response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Get response information string content = Entityutils.tostring (response.getentity ()); return Urldecoder.decode (Content, "Utf-8");}} catch (Exception e) {e.printstacktrace (); return "{\" status\ ": 405,\" resultmsg\ ": \" Network timeout! \"}";} finally {//Releases network connection resource Httpclient.getconnectionmanager (). shutdown ();} Return "{\" status\ ": 405,\" resultmsg\ ": \" Network timeout! \"}";} public static string Getpostresult (string url, list<namevaluepair> paramlist) with parameters passed {urlencodedformentity entity = null;try {entity = new urlencodedformentity (paramlist, "Utf-8");} catch (Unsupportedencodingexception E1) {//TOD O 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 (httpparams);p ost.setentity (entity);//Create Network Access processing object HttpClient httpClient = New Defaulthttpclient (httpparams); try{//Run request parameter HttpResponse response = Httpclient.execute (post);//Infer if the request succeeds if ( Response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Get response information string content = Entityutils.tostring ( Response.getentity (), "UTF-8"); Return Urldecoder.decode (Content, "Utf-8"); }}catch (Exception e) {e.printstacktrace (); return "{\" status\ ": 405,\" Resultms G\ ": \" Network timeout! \"}";} finally {//Releases network connection resource Httpclient.getconnectionmanager (). shutdown ();} Return "{\" status\ ": 405,\" resultmsg\ ": \" Network timeout!\"}";}}
Android: Solving the client's way of getting data garbled from the server