Using JAVA RMI in Android application
Asked Answered
R

3

11

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.

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.

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.

Why that happen? what should I do?

Ramp answered 9/12, 2012 at 15:21 Comment(3)
I think that the library that you are using are not dalvik vm supported.Arrant
more on this is here :-#5322406Arrant
I answered the similar question at this stack post, check it out if you likeMonoculture
B
7

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.

However there are some more disucssions in this post.

Burleson answered 22/1, 2013 at 17:7 Comment(0)
B
8

You can also use the following library 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.

//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.

Berneta answered 7/1, 2015 at 9:17 Comment(2)
please provide more information to solve the problem not just the link to a librarySophronia
Hi @Abhilash! Do you still use LipeRMI? If so, have you faced any difficulties with it till date?Tonie
B
7

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.

However there are some more disucssions in this post.

Burleson answered 22/1, 2013 at 17:7 Comment(0)
F
0

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

Footslog answered 7/1, 2015 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.