开发者社区 问答 正文

安卓接收服务器数据乱码

screenshot
服务端:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
    String name=new String((request.getParameter("name")).getBytes("iso-8859-1"),"utf-8");

    String age=request.getParameter("age");
    PrintWriter pw=response.getWriter();

    sqlConnection sc=new sqlConnection(name,age,pw);
    String encoding = System.getProperty("file.encoding");  
    System.out.println("Encoding:" + encoding);  
    response.setHeader("Content-Type", "application/json;charset=UTF-8");
    response.getWriter().write(new String("试试"));
    System.out.println("name"+name+"age"+age);
}

客户端:

BufferedReader bf=new BufferedReader(new InputStreamReader(hc.getInputStream()));
StringBuffer sb=new StringBuffer();
while((s=bf.readLine())!=null){
sb.append(s);
System.out.println("接收到"+URLDecoder.decode(sb.toString(),"UTF-8"));
    }

展开
收起
蛮大人123 2016-05-27 15:23:29 2460 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    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\":\"网络超时!\"}";            
            }
    }
    2019-07-17 19:18:00
    赞同 展开评论