Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android:解決客戶端從服務器上獲取數據亂碼的方法

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 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\":\"網絡超時!\"}";			
		}
}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved