在 Android 应用程序中使用 JAVA RMI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13788738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:59:20  来源:igfitidea点击:

Using JAVA RMI in Android application

javaandroidrmi

提问by Dave

I've read lots of threads about this issue, and i couldnt see a 'real' solution for it.

我已经阅读了很多关于这个问题的主题,但我看不到它的“真正”解决方案。

I made a java project - which is a rmi server and i have an android application which suppose to be also a rmi client.

我做了一个 java 项目 - 这是一个 rmi 服务器,我有一个 android 应用程序,它假设也是一个 rmi 客户端。

When i checked if the server works I wasn't wise enough to test the client on an android project and i made a test client on a simple java project.

当我检查服务器是否工作时,我没有足够的智慧在一个 android 项目上测试客户端,我在一个简单的 java 项目上做了一个测试客户端。

Now when i'm trying to connect my android application to server i fail because the android project doesn't recognize the java rmi package.

现在,当我尝试将我的 android 应用程序连接到服务器时,我失败了,因为 android 项目无法识别 java rmi 包。

Why that happen? what should I do?

为什么会这样?我该怎么办?

采纳答案by X.X_Mass_Developer

I had the same problem and changed my communication to socket communication!

我遇到了同样的问题,将我的通信更改为套接字通信!

As far as I could figure out Java.rmi unfortunately does not come with Android and therefore it's not possible to use it.

据我所知,不幸的是 Java.rmi 没有与 Android 一起提供,因此无法使用它。

However there are some more disucssions in thispost.

然而,这篇文章还有一些更多的讨论。

回答by Abilash

You can also use the following library LipeRMI

您还可以使用以下库LipeRMI

Here is an example of a Android client interacting with Java Server via LipeRMI. Create the Following 2 classes and a interface for Java application.

下面是一个 Android 客户端通过 LipeRMI 与 Java Server 交互的示例。为 Java 应用程序创建以下 2 个类和一个接口。

//TestService.java
package test.common;

public interface TestService {

    public String getResponse(String data);
}

//TestServer.java
import java.io.IOException;
import java.net.Socket;

import test.common.TestService;

import lipermi.exception.LipeRMIException;
import lipermi.handler.CallHandler;
import lipermi.net.IServerListener;
import lipermi.net.Server;

public class TestServer implements TestService {

    public TestServer() {
        try {
            CallHandler callHandler = new CallHandler();
            callHandler.registerGlobal(TestService.class, this);
            Server server = new Server();
            server.bind(7777, callHandler);
            server.addServerListener(new IServerListener() {

                @Override
                public void clientDisconnected(Socket socket) {
                    System.out.println("Client Disconnected: " + socket.getInetAddress());
                }

                @Override
                public void clientConnected(Socket socket) {
                    System.out.println("Client Connected: " + socket.getInetAddress());
                }
            });
            System.out.println("Server Listening");
        } catch (LipeRMIException | IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getResponse(String data) {
        System.out.println("getResponse called");
        return "Your data: " + data;
    }

}


//TestMain.java
public class TestMain {

    public static void main(String[] args) {
        TestServer testServer = new TestServer();
    }
}

Android client:

安卓客户端:

//MainActivity.java
package com.example.lipermidemoandroidclient;

import java.io.IOException;

import test.common.TestService;

import lipermi.handler.CallHandler;
import lipermi.net.Client;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String serverIP = "192.168.1.231";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnGet = (Button) findViewById(R.id.btnGet);
        btnGet.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                new Conn().execute();
            }
        });

    }

    class Conn extends AsyncTask<Void, Void, MainActivity> {

        @Override
        protected MainActivity doInBackground(Void... params) {
            Looper.prepare();
            try {
                CallHandler callHandler = new CallHandler();
                Client client = new Client(serverIP, 7777, callHandler);
                TestService testService = (TestService) client.getGlobal(TestService.class);
                String msg = testService.getResponse("qwe");
                //Toast.makeText(MainActivity.this, testService.getResponse("abc"), Toast.LENGTH_SHORT).show();
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Looper.loop();
            return null;
        }

    }
}

//TestService.java
package test.common;

public interface TestService {

    public String getResponse(String data);
}

Add the LipeRMI library to both the projects
Make sure you add INTERNET permission in Android project
Also make sure you have the TestService.java file placed in same package name at both places for eg. test.common package here
Also change value of serverIP variable in Android MainActivity.java to the IP of the machine running the Java code.

将 LipeRMI 库添加到两个项目中
确保在 Android 项目中添加了 INTERNET 权限
同时确保将 TestService.java 文件放在两个地方的相同包名中,例如。test.common package here
还要将Android MainActivity.java 中serverIP 变量的值更改为运行Java 代码的机器的IP。

回答by Héctor

Android doesn't support RMI. You should change to socket or raw TCP communication.

Android 不支持 RMI。您应该更改为套接字或原始 TCP 通信。