Android客户端与服务端通信乱码问题的解决
在Android客户端与服务器端通信的过程中,使用输入流,输出流。readUTF(Str)和writeUTF(Str)方法能有效解决乱码问题。
以下为源码例子
客户端:
package com.socket.client;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class main extends Activity implements OnClickListener{
??? /** Called when the activity is first created. */
//定义声明需要用到的UI元素
private EditText edtmsgcontent;
private Button btnSend;
Socket s = null;//声明Socket的引用
DataOutputStream dout = null;//输出流
DataInputStream din = null;//输入流
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? //setContentView(R.layout.main);
??????? InitView();
??? }
??? private void InitView()
??? {
??? //显示主界面
??? setContentView(R.layout.main);
???
??? //通过id获取ui元素对象
??? edtmsgcontent=(EditText)findViewById(R.id.msgcontent);
??? btnSend=(Button)findViewById(R.id.btnsend);
???
??? //为btnsend设置点击事件
??? btnSend.setOnClickListener(this);
??? }
???
??? public void onClick(View bt)
??? {
??? try{//连接网络并打开流
??????? s = new Socket("192.168.10.141", 9998);
??????? dout = new DataOutputStream(s.getOutputStream());
??????? din = new DataInputStream(s.getInputStream());
}catch(Exception e){//捕获异常
e.printStackTrace();//打印异常
}
String u_distext = edtmsgcontent.getText().toString();//描述
String msg = u_distext;
try {
dout.writeUTF(msg);//向服务器发送注册消息
// String msg2 = din.readUTF();//接收服务器发送来的消息
} catch (IOException e) {//捕获异常
e.printStackTrace();//打印异常
} finally{
try{
if(dout != null){
dout.close();
dout = null;
}
}
catch(Exception e){//捕获异常
e.printStackTrace();//打印异常信息
}
try{
if(din != null){
din.close();
din = null;
}
}
catch(Exception e){//捕获异常
e.printStackTrace();//打印异常信息
}
}
???
}}
服务器端:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPDesktopServer implements Runnable{
??? public static final String SERVERIP = "192.168.10.141";
??? public static final int SERVERPORT = 9998;
???
??? DataInputStream din = null;//输入流
DataOutputStream dout = null;//输出流
??? public void run() {
???????? try {
???????????? System.out.println("S: Connecting...");
???????????? ServerSocket serverSocket = new ServerSocket(SERVERPORT);
????????
???????????????? while (true) {
????????
????????????????? Socket client = serverSocket.accept();
?????????????????
????????????????? din=new DataInputStream(client.getInputStream());//输入流
????? ? dout=new DataOutputStream(client.getOutputStream());//输入出流
?????????????????
????????????????? /*
?????????????????? *
?????????????????? *
?????????????????? * 此处增加一个获得用户数据(用户名)的模块。
?????????????????? * 令UserData=(User name)
?????????????????? * 并将其id和用户名加入到List中
?????????????????? *
?????????????????? *
?????????????????? */
????????????????? System.out.println("S: Receiving...");
????????????????? try {
?????????????? ?
??????????????? String msg=din.readUTF();//收消息
????? System.out.println("Client msg = " + msg);
??????????????????? } catch(Exception e) {
??????????????????????? System.out.println("S: Error");
??????????????????????? e.printStackTrace();
??????????????????? } finally {
??????????????????????? client.close();
??????????????????????? System.out.println("S: Done.");
??????????????????? }
???????????? }
????????? } catch (Exception e) {
???????????? System.out.println("S: Error");
???????????? e.printStackTrace();
???????? }
???????
?????????
??? }
?? public static void main (String a[]) {
??????? Thread desktopServerThread = new Thread(new TCPDesktopServer());
??????? desktopServerThread.start();
??? }
}
?
?
注:博客涉及的源码请在千寻资源库:www.qxzyk.com 下载获取,谢谢支持。