標籤:
向server發送HTTP請求。接收到的JSON包為response,用String content = EntityUtils.toString(response.getEntity(),"utf-8");解碼還是出現了中文亂碼,在後面加了
String name = new String(response.getBytes("iso-8859-1"), "UTF-8");
也無濟於事。
想到server好像是用URLENCODER編了碼的。懷著試一試的態度在return後面加了條URLDecoder.decode(content,"utf-8");果然有效!
只是還是不太明確URLDecoder.decode(content,"utf-8")和EntityUtils.toString(response.getEntity(),"utf-8")在解碼的時候有什麼差別。以下是網路端的代碼:
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.SERVER_ADRESS+"UserInfoServlet?";//private static String url_ip = "http://192.168.1.231:8080/indoor/";/** * 釋放資源 */public static void cancel() {Log.i(TAG, "cancel!");// if(conn != null) {// conn.cancel();// }}//無參數傳遞的public static String getPostResult(String url){//建立http請求對象HttpPost post = new HttpPost(url);//建立HttpParams以用來設定HTTP參數 BasicHttpParams httpParams = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParams,10 * 1000);HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000);//建立網路訪問處理對象HttpClient httpClient = new DefaultHttpClient(httpParams);try{//運行請求參數HttpResponse response = httpClient.execute(post);//推斷是否請求成功if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//獲得響應資訊String content = EntityUtils.toString(response.getEntity());return URLDecoder.decode(content,"utf-8");}}catch(Exception e) {e.printStackTrace();return "{\"status\":405,\"resultMsg\":\"網路逾時!\"}";} finally {//釋放網路連接資源httpClient.getConnectionManager().shutdown();}return "{\"status\":405,\"resultMsg\":\"網路逾時!\"}";} //有參數傳遞的public static String getPostResult(String url, List<NameValuePair> paramList){UrlEncodedFormEntity entity = null;try {entity = new UrlEncodedFormEntity(paramList,"utf-8");} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}//建立http請求對象HttpPost post = new HttpPost(url);BasicHttpParams httpParams = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000);HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000);post.setEntity(entity);//建立網路訪問處理對象HttpClient httpClient = new DefaultHttpClient(httpParams);try{//運行請求參數HttpResponse response = httpClient.execute(post);//推斷是否請求成功if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//獲得響應資訊String content = EntityUtils.toString(response.getEntity(),"UTF-8"); return URLDecoder.decode(content,"utf-8"); }}catch(Exception e) {e.printStackTrace();return "{\"status\":405,\"resultMsg\":\"網路逾時!\"}";} finally {//釋放網路連接資源httpClient.getConnectionManager().shutdown();}return "{\"status\":405,\"resultMsg\":\"網路逾時!\"}";}}
Android:解決client從server上擷取資料亂碼的方法