当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>J2ME

有关手机中文问题传输的解决办法


  服务器到客户端:
  下面代码是服务器端把字符写到Client端,经过gbEncoding()方法,所有的字符编码成:\uXXXX.
  
  代码:
  /**
  * Write the String data
  *
  * @param out
  * @param value
  */
  public static void writeUnicode(final DataOutputStream out, final String value) throws ActionException {
  try {
  final String unicode = StringFormatter.gbEncoding( value );
  final byte[] data = unicode.getBytes();
  final int dataLength = data.length;
  
  System.out.println( "Data Length is: " + dataLength );
  System.out.println( "Data is: " + value );
  out.writeInt( dataLength );
  out.write( data, 0, dataLength );
  } catch (IOException e) {
  throw new ActionException( IMDefaultAction.class.getName(), e.getMessage() );
  }
  }
  以下代码是gbEncoding()方法,把双字节字符转换成\uXXXX,ASIIC码在前面补00。
  
  /**
  * This method will encode the String to unicode.
  *
  * @param gbString
  * @return
  */
  
  代码:
  public static String gbEncoding( final String gbString ) {
  char[] utfBytes = gbString.toCharArray();
  String unicodeBytes = "";
  for( int byteIndex = 0; byteIndex < utfBytes.length; byteIndex ++ ) {
  String hexB = Integer.toHexString( utfBytes[ byteIndex ] );
  if( hexB.length() <= 2 ) {
  hexB = "00" + hexB;
  }
  unicodeBytes = unicodeBytes + "\\u" + hexB;
  }
  System.out.println( "unicodeBytes is: " + unicodeBytes );
  return unicodeBytes;
  }
  在客户端收到服务器的数据,先将其一个一个字符解码。双字节显示正常。
  
  代码:
  /**
  * This method will decode the String to a recognized String
  * in ui.
  * @param dataStr
  * @return
  */
  private StringBuffer decodeUnicode( final String dataStr ) {
  int start = 0;
  int end = 0;
  final StringBuffer buffer = new StringBuffer();
  while( start > -1 ) {
  end = dataStr.indexOf( "\\u", start + 2 );
  String charStr = "";
  if( end == -1 ) {
  charStr = dataStr.substring( start + 2, dataStr.length() );
  } else {
  charStr = dataStr.substring( start + 2, end);
  }
  char letter = (char) Integer.parseInt( charStr, 16 ); // 16进制parse整形字符串。
  buffer.append( new Character( letter ).toString() );
  start = end;
  }
  return buffer;
  }
  
  客户端到服务器:
  客户端使用下面方法把手机端的字符编码成ISO-8859-1,传给服务器。
  
  代码:
  /**
  * write the String data
  * @param value
  * @param outData
  */
  private void writeSjis(DataOutputStream outData, String value) {
  try {
  byte[] data = null;
  // data = ( value ).getBytes( "UTF-8" );
  data = ( value ).getBytes( "ISO8859_1" );
  outData.writeInt(data.length);
  outData.write(data, 0, data.length);
  
  System.out.println(" data.length: " + data.length);
  System.out.println(" data.value: " + value);
  } catch (Exception ex) {
  System.out.println(" write error ");
  ex.printStackTrace();
  }
  }
  
  服务器端收到客户端字符流,是用下面方法将其转为UTF-8,以后的操作都是基于UTF-8编码。SQLServer可能会由于内吗不通有不同的变换,所以存取数据库是还要是具体的DB内码作相应的处理。
  
  代码:
  /**
  *
  * @param iso
  * @return
  */
  public static String isoToUtf( final String iso ) {
  String utfString = iso;
  if( iso != null ) {
  try {
  utfString = new String( iso.getBytes( "ISO-8859-1" ), "UTF-8" );
  } catch ( UnsupportedEncodingException e ) {
  utfString = iso;
  }
  } else {
  utfString = "";
  }
  return utfString;
  }
相关内容
赞助商链接