简体   繁体   English

Java RMI - 传递对象

[英]Java RMI - Passing Objects

I'm running a Java program on a Raspberry Pi and an Android-App on a smartphone. 我正在智能手机上的Raspberry PiAndroid-App上运行Java程序。 The app should be able to invoke methods running on the Raspi . 该应用程序应该能够调用在Raspi上运行的Raspi Since Android does not include the standard Java RMI package, I used LipeRMI : http://lipermi.sourceforge.net/ 由于Android不包含标准的Java RMI包,我使用了LipeRMIhttpLipeRMI

So far so good, LipeRMI does work well for passing primitive data types such as boolean or int. 到目前为止, LipeRMI确实可以很好地传递原始数据类型,如boolean或int。

What I want to do now is passing an ArrayList<String> from the Server to the app but every time I run the method, I receive following error: 我现在要做的是将ArrayList<String>从服务器传递给应用程序,但每次运行该方法时,都会收到以下错误:

LipeRMIException:Class java.util.ArrayList is not assignable from control.ServerInt
at lipermi.handler.CallHandler.exportObject(CallHandler.java:54)
at lipermi.handler.CallHandler.exportObject(CallHandler.java:48)
at control.RMIServer.createServer(RMIServer.java:26)
at control.Main.main(Main.java:17)

I do not understand what I'm doing wrong here. 我不明白我在这里做错了什么。

RMIServer.java RMIServer.java

public class RMIServer implements ServerInt{
    private static final long serialVersionUID = 1L;
    private ArrayList<String> list;

    public RMIServer()  {
    list = new ArrayList<String>();
        list.add("50:25:5:-1");
        list.add("99:42:6:4");

        createServer();
    }

    public void createServer() {
        try {
            CallHandler callHandler = new CallHandler();
            callHandler.registerGlobal(ServerInt.class, this);
            callHandler.exportObject(ServerInt.class, list);
            Server server = new Server();
            server.bind(7777, callHandler);

            server.addServerListener(new IServerListener() {

                @Override
                public void clientDisconnected(Socket socket) {}

                @Override
                public void clientConnected(Socket socket) {}
            });

        } catch (LipeRMIException | IOException e) {
            e.printStackTrace();
        }    
    }


    @Override
    public ArrayList<String> getPWMLines() {       
        return list;
    }   
}

ServerInt.java ServerInt.java

import java.util.ArrayList;

public interface ServerInt{          
    public ArrayList<String> getPWMLines();   
}

You obviously misuse the method CallHandler#exportObject(Class, Object) the first parameter must be a class representing an interface and the second parameter must be an instance of a class that implements this interface. 您显然滥用CallHandler#exportObject(Class, Object) ,第一个参数必须是表示接口的类,第二个参数必须是实现此接口的类的实例。 Here you provide an ArrayList as second parameter which is clearly not an instance of ServerInt that is why you get this exception. 在这里,您提供了一个ArrayList作为第二个参数,它显然不是ServerInt一个实例,这就是您获得此异常的原因。

Here is a good example of how to use this method: 以下是如何使用此方法的一个很好的示例:

MyRemoteListener listenerImplementation = new MyRemoteListenerImpl();
callHandler.exportObject(MyRemoteListener.class, listenerImplementation);

In this case the first parameter is the interface MyRemoteListener and the second is an instance of MyRemoteListenerImpl which is an implementation of the interface MyRemoteListener . 在这种情况下,第一个参数是接口MyRemoteListener和第二个是一个实例MyRemoteListenerImpl这是接口的实现MyRemoteListener


To expose your List you can try callHandler.registerGlobal(List.class, list) 要公开你的List你可以尝试callHandler.registerGlobal(List.class, list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM