Android的插座+ ObjectOutputStream的工作不正常 [英] Android Socket + ObjectOutputStream not working correctly

查看:124
本文介绍了Android的插座+ ObjectOutputStream的工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,伙计们。我正在开发一个客户机/服务器程序,其中客户端是一款Android设备。 服务器具有读取从输入流的对象的监听器类。我创建了一个客户端软件发送一个小物件在本地网络中的另一台计算机。 电脑对电脑工作完全正常,我读了对象,并打印的内容。但是,的的code移植到Android不工作,(我以防万一重写了它)。我构造对象(ANY对象),这是可序列化的,并通过用ObjectOutputStream发送它。在计算机上运行我的服务器没有连接到设备,但它给了我一个ClassNotFound的异常,即使即时打印对象(其中有一个toString)。正如我所说的,在另一台计算机在同一code运行时(作为一个.jar文件)工作完全正常。

Hey guys. 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.

这是很奇怪的一部分,如果我发送一个布尔值或字符串(从设备)工程....它只是我的自定义是不对象。我presume这将适用于任何标准Java对象。

如果您发现错误,请记住,在code没有工作,但只能从另一台计算机到我的电脑...不是在Android设备连接至计算机。如果你还是找另一个明显的错误,那么真棒:)

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程序:

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();
        }

    }
}

谢谢!

推荐答案

我怀疑Android的序列化格式可以与Java虚拟机的格式不兼容。你可以尝试转换你的对象到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?

这篇关于Android的插座+ ObjectOutputStream的工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆