Android:解決client從server上擷取資料亂碼的方法

來源:互聯網
上載者:User

標籤:

向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上擷取資料亂碼的方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.