安卓APP输入的中文在传到服务端时出现个别中文乱码,请帮忙看看能否改善?谢谢~ 80C

安卓应用程序输入的中文通过Socket传到服务端时偶尔出现个别中文乱码:
���常生产,于风���,王��诚

安卓客户端代码如下:

public String SendMsg(String Message) {

        // 检查网络
        if (!CommonHelper.IsWIFIConnected(CrashHandler.getInstance().getContext())){
            Toast toast = Toast.makeText(CrashHandler.getInstance().getContext(),"Wifi is NOT connected yet", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            toast.show();
            return "";
        }

        String rtnMessage = "";
        try {

            // outgoing stream redirect to socket
            Socket socket = new Socket(ContentFlag.SERVER_IP, ContentFlag.SERVER_PORT);

            OutputStream out = socket.getOutputStream();

            // 注意第二个参数据为true将会自动flush,否则需要需要手动操作
            out.flush();
            PrintWriter output=new PrintWriter(new OutputStreamWriter(out, Charset.forName("UTF-8")), true);
            // 发送消息
            output.println(Message);

            // Read from server
            BufferedReader input = new BufferedReader(new InputStreamReader(
                    socket.getInputStream(), "UTF-8"));
            rtnMessage = input.readLine();
            // close socket ;
            socket.close();

        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rtnMessage;

    }

服务端代码如下(C#):
public void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];

        // Establish the local endpoint for the socket.

        IPAddress ipAddress = IPAddress.Parse("172.22.203.100");

        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

        // Create a TCP/IP socket.
        Socket listener = new Socket(
                                      AddressFamily.InterNetwork,
                                      SocketType.Stream,
                                      ProtocolType.Tcp
                                      );


        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (true)
            {
                try
                {
                    Socket socket = listener.Accept();
                    new Thread(new ParameterizedThreadStart(ProcessSocket)).Start(socket);
                }
                catch 
                {
                    //StaticLogger.Write(e.Message.ToString(), LogLevel.L5_FAIL);
                }
            }
        }
        catch(Exception ex)
        {
            String sUnHandleMsg = "Socket Listen Error=" + ex.Message.ToString() + "IStackTrace=" + ex.StackTrace.ToString();
            StaticLogger.Write(sUnHandleMsg, androidServer.Logger.LogLevel.L5_FAIL);
        }

    }


    public void ProcessSocket(object socket)
    {
        Socket recSocket = (Socket)socket;

        String data = null;

        byte[] bytes = null;

        while (true)
        {
            bytes = new byte[recSocket.Available];

            try
            {
                int bytesRec = recSocket.Receive(bytes);
                data += Encoding.UTF8.GetString(bytes, 0, bytesRec);
                if (data.IndexOf("\n") > -1)
                {
                    break;
                }
            }
            catch(Exception ee)
            {
                //通信异常,Socket被强制中断
                //StaticLogger.Write(ee.Message, LogLevel.L5_FAIL);
            }
        }
    }

2个回答

建议你把Android端的中文用 URLEncoder.encode("中文","utf-8") 转码,然后发送,发送完之后 URLDecoder.decode("解码内容","utf-8") 解码就行,返回中文,然后处理。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问