0

在您将此标记为重复之前,请听我说完。我已经从 StackOverflow 的成员那里得到了以前的建议,但我仍然无法让它工作,但我已经改进了我的代码并限制了可能是什么问题,但我不明白我做错了什么。仅供参考:这是我正在制作的客户端-服务器聊天程序的客户端。我已经制作了完全正常运行的服务器和客户端的 PC 版本。

关于我认为可能是问题的根源。如果您查看 ServerTask 类(按下连接按钮时执行),它看起来好像没有启动套接字?有人可以广泛帮助我吗?如果有人可以帮助这个绝望的初学者,将不胜感激。

这可能无济于事,但这里也是 AndroidManifest.xml 因为我在某个地方看到我需要设置权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.JurkoAndroidChat"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="16"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">

        <activity android:name="MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>




package com.example.JurkoAndroidChat;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;

import java.net.*;
import java.io.*;
import java.util.*;



public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    // Right here, we connecting the components of the main.xml form to code
    Button connectButton, disconnectButton, sendButton;
    TextView chatArea, clientArea;
    EditText messageField, usernameField, ipField;

    //Extra variables and sockets
    String username, serverIP;
    int Port = 5000;
    Socket sock;
    PrintWriter out;
    BufferedReader in;
    ArrayList<String> userList;
    Boolean isConnected = false;
    ServerTask serverTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        System.out.println("Working?");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        userList = new ArrayList();
        connectButton = (Button)findViewById(R.id.button);
        sendButton = (Button)findViewById(R.id.button1);
        disconnectButton = (Button)findViewById(R.id.button2);

        chatArea = (TextView)findViewById(R.id.textView2);
        clientArea = (TextView)findViewById(R.id.textView3);

        messageField = (EditText)findViewById(R.id.editText2);
        usernameField = (EditText)findViewById(R.id.editText);
        ipField = (EditText)findViewById(R.id.editText1);



        connectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //To change body of implemented methods use File | Settings | File Templates.
                if (isConnected == false) {
                    username = usernameField.getText().toString();
                    usernameField.setFocusable(false);
                    usernameField.setClickable(false);
                    serverIP = ipField.getText().toString();
                    ipField.setFocusable(false);
                    ipField.setClickable(false);
                    serverTask = new ServerTask();
                    serverTask.execute();


                } else if (isConnected == true) {
                    chatArea.append("You are already connected to the server.\n");
                }
            }
        });

        disconnectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //To change body of implemented methods use File | Settings | File Templates.
                String bye = (username + ": :Disconnect");
                try {
                    out.print(bye);
                    out.flush();
                    chatArea.append("Disconnected.\n");
                    sock.close();

                } catch (Exception e) {e.printStackTrace();}
                isConnected = false;
                usernameField.setFocusable(true);
                usernameField.setClickable(true);
                ipField.setFocusable(true);
                ipField.setClickable(true);
                clientArea.setText("");
            }
        });

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //To change body of implemented methods use File | Settings | File Templates.
                String nothing = "";
                if ((messageField.getText().toString().equals(nothing))) {
                    messageField.setText("");
                    messageField.requestFocus();
                } else {
                    try {
                        out.println(username + ":" + messageField.getText().toString() + ":" + "Chat");
                        out.flush();
                        serverTask.printToStream("hey");

                    } catch (Exception e) {
                        chatArea.append("Message was not sent.\n" + e);
                    }
                    messageField.setText("");
                    messageField.requestFocus();
                }
            }
        });


    }

    public class ServerTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                Log.i("Asynctask", "doInBackground");
                sock = new Socket(serverIP, Port);
                out = new PrintWriter(sock.getOutputStream());
                in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                out.println(username + ":" + "has connected." + ":" + "Connect");
                out.flush();
                isConnected = true;

            } catch (Exception ex) {
//                    chatArea.append("Unable to connect to " + serverIP + " at port " + Port + "." + ex);
//                    ex.printStackTrace();
//                    usernameField.setFocusable(true);
//                    usernameField.setClickable(true);
//                    ipField.setFocusable(true);
//                    ipField.setClickable(true);

            }
                new streamTask().execute();
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public void printToStream(String message) {
            try {
                out.println(message);
                out.flush();
            } catch (Exception e) {chatArea.append(e + "\n"); }


        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
           //To change body of overridden methods use File | Settings | File Templates.
        }

    }

    public class streamTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... voids) {
            String[] data;
            String stream, done = "Done", connect = "Connect", disconnect = "Disconnect", chat = "Chat";

            try {
                while ((stream = in.readLine()) != null) {
                    data = stream.split(":");

                    if (data[2].equals(chat)) {
                        chatArea.append(data[0] + ": " + data[1] + '\n');
                    } else if (data[2].equals(connect)) {
                        chatArea.setText("");
                        userAdd(data[0]);
                    } else if (data[2].equals(disconnect)) {
                        userRemove(data[0]);
                    } else if (data[2].equals(done)) {
                        clientArea.setText("");
                        writeUsers();
                        userList.clear();
                    }


                }
            }  catch (Exception e) {e.printStackTrace();}
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }
    }

    public void executeTask() {
        new ServerTask().execute();
    }

    public class IncomingReader implements Runnable {
        @Override
        public void run() {
            //To change body of implemented methods use File | Settings | File Templates.

        }
    }

    public void ListenThread() {
        Thread IncomingReader = new Thread(new IncomingReader());
        IncomingReader.start();
    }



    public void userAdd(String data) {
        userList.add(data);
    }

    public void userRemove(String data) {
        chatArea.append(data + " has disconnected from the server.\n");
        for (String token:userList)
            if (token.equals(data))
                userList.remove(token);
    }

    public void writeUsers() {
        String[] tempList = new String[(userList.size())];
        userList.toArray(tempList);
        for (String token:tempList) {
            clientArea.append(token + '\n');
        }
    }








}
4

2 回答 2

0

由于没有日志,因此不确定您的问题到底是什么(我建议您提供日志以便我们为您提供帮助),但是当我查看您的代码时,我可以看到有一个大问题: 您尝试更新 UI在后台线程中

查看这些代码行:

protected Void doInBackground(Void... voids) {
            String[] data;
            String stream, done = "Done", connect = "Connect", disconnect = "Disconnect", chat = "Chat";

            try {
                while ((stream = in.readLine()) != null) {
                    data = stream.split(":");

                    if (data[2].equals(chat)) {
                        chatArea.append(data[0] + ": " + data[1] + '\n');
                    } else if (data[2].equals(connect)) {
                        chatArea.setText("");
                        userAdd(data[0]);
                    } else if (data[2].equals(disconnect)) {
                        userRemove(data[0]);
                    } else if (data[2].equals(done)) {
                        clientArea.setText("");
                        writeUsers();
                        userList.clear();
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null; // To change body of implemented methods use File |
                            // Settings | File Templates.
        }

在这个 doInBackground 中,有些地方会更新 UI,它在后台线程中执行,会导致异常。
阅读这篇文章,这可能会有所帮助https://developer.android.com/training/multiple-threads/communicate-ui.html

于 2013-10-10T00:25:11.923 回答
0

将这里留给所有遇到 AsyncTask 问题的人,因为它可能听起来令人困惑,但它确实有效。我终于让它工作了,我不知道为什么 localhost 不能工作,因为它是我正在测试的物理设备(Samsung Galaxy Tab 2 7.0)。我决定尝试我的实际外部 IP,它终于奏效了。

构建您的应用程序,以便在 doInBackground 中完成物理网络内容(无 GUI)

于 2013-10-10T01:02:58.600 回答