zoukankan      html  css  js  c++  java
  • Android:解决client从server上获取数据乱码的方法

    向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 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":"网络超时!

    "}"; } }



  • 相关阅读:
    测试用例设计--边界值
    测试用例设计--等价类划分
    实战JAVA虚拟机 JVM故障诊断与性能优化(八)
    Centos-Mysql远程访问
    实战JAVA虚拟机 JVM故障诊断与性能优化(七)
    实战JAVA虚拟机 JVM故障诊断与性能优化(六)--->JConsole And Visual VM
    实战JAVA虚拟机 JVM故障诊断与性能优化(六)
    jdk 1.8 VisualVM 插件 地址 变更
    实战JAVA虚拟机 JVM故障诊断与性能优化(七)--->无处不在的字符串:String在虚拟机中的实现
    本地git项目,push到github上
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5144042.html
Copyright © 2011-2022 走看看