Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的服务类接收空值?_Java_Android_Networking_Service - Fatal编程技术网

Java 为什么我的服务类接收空值?

Java 为什么我的服务类接收空值?,java,android,networking,service,Java,Android,Networking,Service,我有一个PlayerActivity类和一个PlayerConnect类 之前,我使用了PlayerActivity类来启动一个线程,该线程将运行PlayerConnect。我最近发现线程行为有点不正常,建议使用服务(为此:IntentServices) 问题:我的日志告诉我IP、name和init为null、null、0,但情况不应该是这样,因为它们应该在启动服务之前设置。日志还告诉我它无法连接到本地主机,我认为它默认为本地主机,因为它没有一个可尝试的设置IP 我的意图中是否正确引用了Play

我有一个
PlayerActivity
类和一个
PlayerConnect

之前,我使用了
PlayerActivity
类来启动一个线程,该线程将运行
PlayerConnect
。我最近发现线程行为有点不正常,建议使用服务(为此:IntentServices)

问题:我的日志告诉我IP、name和init为null、null、0,但情况不应该是这样,因为它们应该在启动服务之前设置。日志还告诉我它无法连接到本地主机,我认为它默认为本地主机,因为它没有一个可尝试的设置IP

我的意图中是否正确引用了
PlayerConnect

玩家活动:

public class PlayerActivity extends AppCompatActivity {

    InetAddress hostIP;
    String playerName;
    int playerInitiative = -1;
    boolean denied = false;
    boolean started = false;
    PlayerConnect playerConnect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        submit = (Button) findViewById(R.id.submit);

        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!(playerName.equals("")) && playerInitiative > -1) {
                    try {
                        if (!(hostIPString.equals(""))) {
                            hostIP = InetAddress.getByName(hostIPString);
                            if(!(started)) {
                                playerConnect = new PlayerConnect();
                                playerConnect.SetHostIP(hostIP);
                                playerConnect.SetPlayerName(playerName);
                                playerConnect.SetPlayerInit(playerInitiative);
                                denied = playerConnect.GetDenied();
                                started = true;
                            }

                            else {
                                playerConnect.SetHostIP(hostIP);
                                playerConnect.SetPlayerName(playerName);
                                playerConnect.SetPlayerInit(playerInitiative);
                                denied = playerConnect.GetDenied();
                            }


                            if (denied)
                            {
                                started = false;
                            }

                            else
                            {
                                Intent playerConnectIntent =
                                        new Intent(PlayerActivity.this, playerConnect.getClass());
                                startService(playerConnectIntent);
                            }
                        }
                    }

                    catch (Exception e) {
                        Log.i("LOG", e.toString());
                    }
                }
            }
        });
    }
}
public class PlayerConnect extends IntentService {

    public PlayerConnect() {
        super("PlayerConnect");
    }

    InetAddress hostIP;
    String playerName;
    int playerInitiative;
    boolean denied = false;

    @Override
    public void onHandleIntent(Intent intent) {
            SendPlayerData(hostIP, playerName, playerInitiative);
    }

    private void SendPlayerData(InetAddress IP, String name, int init) {
        try {
            int port = 8080;
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(IP, port), 3000);
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());

            if (socket.isConnected())
            {
                output.writeUTF(name);
                output.writeInt(init);
                output.close();
                socket.close();

                Log.i("LOG", "client socket connected");
            }

            if (socket.isClosed())
            {
                stopSelf();

                Log.i("LOG", "client socket closed");
            }
        }

        catch (Exception e) {
            denied = true;
            Log.i("LOG", e.toString());
            Log.i("LOG", IP + name + init);

        }
    }

    public void SetHostIP(InetAddress host)
    {
        hostIP = host;
    }

    public void SetPlayerName(String name)
    {
        playerName = name;
    }

    public void SetPlayerInit(int init)
    {
        playerInitiative = init;
    }

    public boolean GetDenied()
    {
        return denied;
    }
}
玩家连接:

public class PlayerActivity extends AppCompatActivity {

    InetAddress hostIP;
    String playerName;
    int playerInitiative = -1;
    boolean denied = false;
    boolean started = false;
    PlayerConnect playerConnect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        submit = (Button) findViewById(R.id.submit);

        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!(playerName.equals("")) && playerInitiative > -1) {
                    try {
                        if (!(hostIPString.equals(""))) {
                            hostIP = InetAddress.getByName(hostIPString);
                            if(!(started)) {
                                playerConnect = new PlayerConnect();
                                playerConnect.SetHostIP(hostIP);
                                playerConnect.SetPlayerName(playerName);
                                playerConnect.SetPlayerInit(playerInitiative);
                                denied = playerConnect.GetDenied();
                                started = true;
                            }

                            else {
                                playerConnect.SetHostIP(hostIP);
                                playerConnect.SetPlayerName(playerName);
                                playerConnect.SetPlayerInit(playerInitiative);
                                denied = playerConnect.GetDenied();
                            }


                            if (denied)
                            {
                                started = false;
                            }

                            else
                            {
                                Intent playerConnectIntent =
                                        new Intent(PlayerActivity.this, playerConnect.getClass());
                                startService(playerConnectIntent);
                            }
                        }
                    }

                    catch (Exception e) {
                        Log.i("LOG", e.toString());
                    }
                }
            }
        });
    }
}
public class PlayerConnect extends IntentService {

    public PlayerConnect() {
        super("PlayerConnect");
    }

    InetAddress hostIP;
    String playerName;
    int playerInitiative;
    boolean denied = false;

    @Override
    public void onHandleIntent(Intent intent) {
            SendPlayerData(hostIP, playerName, playerInitiative);
    }

    private void SendPlayerData(InetAddress IP, String name, int init) {
        try {
            int port = 8080;
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(IP, port), 3000);
            DataOutputStream output = new DataOutputStream(socket.getOutputStream());

            if (socket.isConnected())
            {
                output.writeUTF(name);
                output.writeInt(init);
                output.close();
                socket.close();

                Log.i("LOG", "client socket connected");
            }

            if (socket.isClosed())
            {
                stopSelf();

                Log.i("LOG", "client socket closed");
            }
        }

        catch (Exception e) {
            denied = true;
            Log.i("LOG", e.toString());
            Log.i("LOG", IP + name + init);

        }
    }

    public void SetHostIP(InetAddress host)
    {
        hostIP = host;
    }

    public void SetPlayerName(String name)
    {
        playerName = name;
    }

    public void SetPlayerInit(int init)
    {
        playerInitiative = init;
    }

    public boolean GetDenied()
    {
        return denied;
    }
}
你给了我很多帮助,所以我要把它作为未来读者的答案谢谢。


不,您永远不想自己构造服务子类(newplayerconnect())。这是系统的工作。它为响应startService()调用而提供的数据与从构造函数返回的数据完全不同,因此它不会包含任何您添加到其中的数据。您需要将数据添加到PlayerConnectionContent,并在OnHandleContent()中检索数据


不,您永远不希望自己构建
服务
子类(
newplayerconnect()
)。这是系统的工作。它为响应
startService()
调用而提供的数据与从构造函数返回的数据完全不同,因此它不会包含任何您添加到其中的数据。您需要将数据添加到
playerConnectIntent
,然后在
onHandleIntent()
中检索数据。先看看。