json在客户端和服务器端传中文乱码
HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(strUrl); StringEntity entity = new StringEntity(json); entity.setContentType("application/json"); entity.setContentEncoding("utf-8"); post.setEntity(entity); HttpResponse responString = client.execute(post);
服务器端接受数据:
Java code?try { BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream())); String line = ""; StringBuilder sb = new StringBuilder(); while((line = br.readLine())!=null){ sb.append(line); } System.out.println("diaryjson:"+sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("diaryjson receive failure!!!!"); } URLDecoder.decode是解码把。 把发送的数据用URLEncoder.encode编码下,在接收到时再用URLDecoder.decode解码下
return URLDecoder.decode(diaryjson,"utf-8");//这句是解码代码,发送时用编码
|