1. 程式人生 > >Android:解決客戶端從伺服器上獲取資料亂碼的方法

Android:解決客戶端從伺服器上獲取資料亂碼的方法

向伺服器傳送HTTP請求,接收到的JSON包為response,用String content = EntityUtils.toString(response.getEntity(),"utf-8");解碼還是出現了中文亂碼,在後面加了
        String name = new String(response.getBytes("iso-8859-1"), "UTF-8");  

也無濟於事。想到伺服器好像是用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 block
				e1.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\":\"網路超時!\"}";			
		}
}