Android tcp客户端应用程序连接崩溃

时间:2015-03-20 09:40:53

标签: java android tcp

我通过登录和注册开发了一个简单的聊天应用程序。 对于通信,我使用TCP客户端和TCP服务器。 如果用户单击登录,我将连接到服务器。 现在我需要检查是否连接。如果返回true,我开始一个新的意图。 我没有root用android手机查看日志,我的电脑对于模拟器来说很低,但也许你可以在源代码中看到失败。

如果加载的进度对话框是应用程序崩溃。也许我需要一个例外..

登录:

public void LoginUser() {
    class AttemptLogin extends AsyncTask<String, Void, Boolean> {
        ProgressDialog pDialog;
        private Context context;

        public AttemptLogin(Activity activity) {
            context = activity;
            pDialog = new ProgressDialog(context);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog.setMessage(context.getString(R.string.login) + "...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... args) {
            session = new Session(getBaseContext(), "Azcaq", "Azcaq $$", "Test");
            session.client = new TcpClient(new TcpClient.OnMessageReceived() {
                @Override
                public void messageReceived(String message) {

                }
            });
            session.client.run();
            Login();
            return null;
        }

        protected void onPostExecute(final Boolean success) {
            pDialog.dismiss();
        }
    }

    new AttemptLogin(this).execute();
}

public void Login() {
    if(session.client.isConnected()) {
        session.setLogin(true);
        Intent intent = new Intent(this, ChatsActivity.class);
        startActivity(intent);
        finish();
    } else {
        Toast.makeText(getApplicationContext(), getString(R.string.connectionfail), Toast.LENGTH_LONG).show();
    }
}

TcpClient的

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class TcpClient {
    public static final String SERVER_IP = "82.168.0.198";
    public static final int SERVER_PORT = 64000;
    private String mServerMessage;
    private OnMessageReceived mMessageListener = null;
    private boolean mRun = false;
    private PrintWriter mBufferOut;
    private BufferedReader mBufferIn;
    private Boolean connected = false;

    public TcpClient(OnMessageReceived listener) {
        mMessageListener = listener;
    }

    public void sendMessage(String message) {
        if (mBufferOut != null && !mBufferOut.checkError()) {
            mBufferOut.println(message);
            mBufferOut.flush();
        }
    }

    public Boolean isConnected() {
        if(connected) {
            return true;
        }
        return false;
    }

    public void stopClient() {
        mRun = false;
        if (mBufferOut != null) {
            mBufferOut.flush();
            mBufferOut.close();
        }

        mMessageListener = null;
        mBufferIn = null;
        mBufferOut = null;
        mServerMessage = null;
        connected = false;
    }

    public void run() {
        mRun = true;
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            Socket socket = new Socket(serverAddr, SERVER_PORT);
            try {
                mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                if(socket.isConnected()) {
                    connected = true;
                }

                while (mRun) {
                    try {
                        mServerMessage = mBufferIn.readLine();
                        if (mServerMessage != null && mMessageListener != null) {
                            mMessageListener.messageReceived(mServerMessage);
                        }
                    } catch (SocketTimeoutException e) {
                    } catch (IOException e) {
                    }
                }
            } catch (SocketException e) {
            } catch (Exception e) { } finally {
                socket.close();
            }
        } catch (Exception e) {
        }
    }

    public interface OnMessageReceived {
        public void messageReceived(String message);
    }
}

0 个答案:

没有答案