Android 套接字 + ObjectOutputStream 无法正常工作

发布于 2024-09-27 16:28:28 字数 3663 浏览 3 评论 0原文

我正在开发一个客户端/服务器程序,其中客户端是 Android 设备。

服务器有一个从输入流读取对象的侦听器类。我为另一台计算机创建了一个客户端软件,该软件通过本地网络发送一个小对象。 计算机到计算机工作得很好,我读取了该对象并打印了内容。然而,移植到 android 的相同代码(我重写了它以防万一)不起作用。我构造一个可序列化的对象(任何对象),并通过 ObjectOutputStream 发送它。我在计算机上运行的服务器确实连接到了设备,但它给了我一个 ClassNotFound 异常,即使我正在打印对象(其中有一个 toString)。正如我所说,在另一台计算机上运行的相同代码(作为 .jar 文件)运行得很好。

这是非常奇怪的部分,如果我发送一个布尔值或字符串(从设备)它会起作用......它只是我的“自定义”对象,但不能。我认为这适用于任何“标准”java 对象。

如果您确实发现错误,请记住该代码确实有效,但只能从另一台计算机到我的计算机...而不是 Android 设备到计算机。如果您仍然发现另一个明显的错误,那就太棒了:)

ANDROID 程序:

package WaitDroid.Main;

import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity {
/** Called when the activity is first created. */
private Button a;
private TextView x;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.a = (Button) this.findViewById(R.id.Send_Order);
    this.x = (TextView) this.findViewById(R.id.TextView1);
    this.a.setOnClickListener(new OnClickListener()
    {
        
        @Override
        public void onClick(View arg0)
        {
            sendMenu();
        }
    });
}

private void sendMenu()
{
     try
     {
         InetAddress serverAddress = InetAddress.getByName("128.153.180.109");
         Socket socket = new Socket(serverAddress, 4322);
         ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
         TestObject send = new TestObject("Hi", 32);
         out.writeObject(send);
         out.close();
         socket.close();
         }
         catch(Exception e)
         {
         x.setText(e.getMessage());
         }
    }
}

测试对象:

package WaitDroid.Main;

import java.io.Serializable;

public class TestObject implements Serializable
{
    private String name;
    private int number;

    public TestObject(String a, int b)
    {
        name = a;
        number = b;
    }

    public String toString()
    {
        return name +" - "+ number;
    }
}

服务器监听器:

package Main;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectInputStream.GetField;
import java.net.ServerSocket;
import java.net.Socket;

import Order.Order;

public class ServerListener extends Thread 
{

    public void run() {
        try {
            ServerSocket listen = new ServerSocket(4322);

            while (true) {
                Socket socket = listen.accept();
                String clientInetAddr = socket.getInetAddress().toString();
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

                System.out.println("Connected to: " + clientInetAddr);
                
                try
                {
                    Object a = in.readObject();
                    System.out.println(a);
                    //RestaurantServerRun.n.server.addOrder(a);
                }
                
                catch(IOException e)
                {
                    System.err.println(e.getMessage());
                }

                in.close();
                socket.close();
            }
        }
        catch (Exception e) {
            System.err.println("Error in run()");
            e.printStackTrace();
        }
        
    }
}

谢谢!

I am developing a client/server program where the client is an android device.

The server has a listener class that reads an object from the input stream. I created a client software for another COMPUTER that sends a small object over a local network. Computer to Computer works perfectly fine, i read the object and printed the contents. However, the SAME code ported over to android (I rewrote it just in case) does not work. I construct an object (ANY object), which is serializable, and send it via the ObjectOutputStream. My server running on the computer does connect to the device, but it gives me a ClassNotFound exception, even if im printing the object (Which has a toString). As i said, the same code running on another COMPUTER (as a .jar file) works perfectly fine.

Here is the really strange part, if i send a boolean or String (from the device) it works....its just my "custom" objects that dont. I presume this would work for any "standard" java object.

If you do find mistake please keep in mind that the code does work, but only from another computer to my computer...not the Android device to the Computer. If you still find another glaring mistake, then awesome :)

ANDROID PROGRAM:

package WaitDroid.Main;

import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity {
/** Called when the activity is first created. */
private Button a;
private TextView x;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.a = (Button) this.findViewById(R.id.Send_Order);
    this.x = (TextView) this.findViewById(R.id.TextView1);
    this.a.setOnClickListener(new OnClickListener()
    {
        
        @Override
        public void onClick(View arg0)
        {
            sendMenu();
        }
    });
}

private void sendMenu()
{
     try
     {
         InetAddress serverAddress = InetAddress.getByName("128.153.180.109");
         Socket socket = new Socket(serverAddress, 4322);
         ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
         TestObject send = new TestObject("Hi", 32);
         out.writeObject(send);
         out.close();
         socket.close();
         }
         catch(Exception e)
         {
         x.setText(e.getMessage());
         }
    }
}

TEST OBJECT:

package WaitDroid.Main;

import java.io.Serializable;

public class TestObject implements Serializable
{
    private String name;
    private int number;

    public TestObject(String a, int b)
    {
        name = a;
        number = b;
    }

    public String toString()
    {
        return name +" - "+ number;
    }
}

SERVER LISTENER:

package Main;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectInputStream.GetField;
import java.net.ServerSocket;
import java.net.Socket;

import Order.Order;

public class ServerListener extends Thread 
{

    public void run() {
        try {
            ServerSocket listen = new ServerSocket(4322);

            while (true) {
                Socket socket = listen.accept();
                String clientInetAddr = socket.getInetAddress().toString();
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

                System.out.println("Connected to: " + clientInetAddr);
                
                try
                {
                    Object a = in.readObject();
                    System.out.println(a);
                    //RestaurantServerRun.n.server.addOrder(a);
                }
                
                catch(IOException e)
                {
                    System.err.println(e.getMessage());
                }

                in.close();
                socket.close();
            }
        }
        catch (Exception e) {
            System.err.println("Error in run()");
            e.printStackTrace();
        }
        
    }
}

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-10-04 16:28:28

我怀疑Android的序列化格式可能与Java VM的格式不兼容。您可以尝试将对象转换为 XML 或其他文本格式吗?

I suspect Android's serialization format may be not compatible with Java VM's format. Can you try converting your objects to XML or some other text format?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文