Using JAVA RMI in Android application - java

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?

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.

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.

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

Related

Use Infura to run a IPFS node in Java Android

I am trying to connect to an Infura node from Java Android application.
I was following these documents to connect to an infura node.
https://kauri.io/managing-storage-in-a-java-application-with-ipfs/3e8494f4f56f48c4bb77f1f925c6d926/a
https://github.com/ipfs-shipyard/java-ipfs-http-client/issues/115
Code:
package com.example.javahttp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import io.ipfs.api.IPFS;
public class MainActivity extends AppCompatActivity {
IPFS ipfs ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTask().execute();
}
private class MyTask extends AsyncTask<Void , Void, Void > {
#Override
protected Void doInBackground(Void... voids) {
IPFS ipfs = new IPFS("/dnsaddr/ipfs.infura.io/tcp/5001/https");
try{
System.out.println("connected");
System.out.println("id: "+ ipfs.id());
}catch(Exception e){
System.out.println("not connected"+e);
}
return null;
}
}
}
I am getting this error.
java.lang.RuntimeException: IOException contacting IPFS daemon.
Trailer: null ipfs method not allowed
Any suggestions please.
I don't get why there is such an error but when I used a plain java class to connect with it. It was possible ,you may refer here to my repository blockchain with java to see if you have all the relevant dependencies and you have been doing it properly because I am not familiar with android but I am familiar with web3j.
You can use the below code to connect with an infura node and parse a file to it.
import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class App {
public static void main(String[] args) throws IOException {
IPFS ipfs = new IPFS("/dnsaddr/ipfs.infura.io/tcp/5001/https");
try {
NamedStreamable.InputStreamWrapper is = new NamedStreamable.InputStreamWrapper(new FileInputStream());
MerkleNode response = ipfs.add(is).get(0);
} catch (IOException ex) {
throw new RuntimeException("Error whilst communicating with the IPFS node", ex);
}
}
}
In case anyone has trouble with this nowadays, the fix to this issue was simply to upgrade to the newest version.
I upgraded from v1.2.3 to v1.3.3 and the issue disappeared.
For maven, pom.xml:
<dependency>
<groupId>com.github.ipfs</groupId>
<artifactId>java-ipfs-http-client</artifactId>
<version>v1.3.3</version>
</dependency>

How to connect sockets and receive Data?

I am new to sockets, I was hoping someone knows how to connect sockets on Android Studio and receive data.
This is the websocket which uses socket.io that I am trying to receive data from: https://ws-api.iextrading.com/1.0/stock/aapl/quote
This is a link to the API which explains the websocket: https://iextrading.com/developer/docs/#websockets
I am trying to get realtime stock data for my app, but I do not know how to connect to the socket then receive the data and my app crashes before it even opens, this is my code so far:
package com.example.android.stocksApp;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private static Socket s;
private static InputStreamReader isr;
private static BufferedReader br;
TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView)findViewById(R.id.result);
stockData();
}
public void stockData (){
myTask mTask = new myTask();
mTask.execute();
}
class myTask extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
try {
s = new Socket("https://ws-api.iextrading.com/1.0/stock/aapl/quote", 80);
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String quote = br.readLine();
result.setText(quote);
s.close();
is.close();
isr.close();
br.close();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
}
}
You're trying to connect to a Web service. Web services may return either a XML or JSON. The actual Socket library isn't needed nor used for this. This specific web service returns a JSON, so what you need is a library that is capable of making a GET request to this URL and parse the response. For Android, I recommend using Volley. You can include Volley in your project by adding this line to your build.gradle.
implementation 'com.android.volley:volley:1.0.0'
This question might help you get started.

Error with latest jar of Nanohttpd

I followed the SO for setting NanoHttpd to serve files from here - How to serve a mp3 file using latest NanoHTTPD 2.3.0 in Android?
This works but I require the use the latest version from Github, because it handles more HTTP methods and is required for project.
I built the jar locally and added and compiled the APK. The web server initializes but every request is returned as Not Found. Nothing else. There is no log for that as well to see the problem. What is going on?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Map;
import android.util.Log;
import org.nanohttpd.protocols.http.NanoHTTPD;
import org.nanohttpd.protocols.http.response.Response;
import org.nanohttpd.protocols.http.response.Status;
import org.nanohttpd.protocols.http.request.Method;
import static org.nanohttpd.protocols.http.response.Response.newChunkedResponse;
public class MainActivity extends AppCompatActivity {
public StackOverflowMp3Server server;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
server = new StackOverflowMp3Server();
try {
server.start();
} catch(IOException ioe) {
Log.w("Httpd", "The server could not start.");
}
Log.w("Httpd", "Web server initialized.");
}
#Override
public void onDestroy()
{
super.onDestroy();
if (server != null)
server.stop();
}
public class StackOverflowMp3Server extends NanoHTTPD {
public StackOverflowMp3Server() {
super(8089);
}
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
String answer = "";
Log.w("HTTPD", uri);
Log.w("HTTPD", parameters.toString());
Log.w("HTTPD", "Method is: "+method.toString());
Log.w("HTTPD", "Header is: "+header.toString());
FileInputStream fis = null;
try {
fis = new FileInputStream("/storage/C67A-18F7/"
+ "/Music/"+uri);
Log.w("HTTPD", uri + " found");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newChunkedResponse(Status.OK, "audio/mpeg", fis);
}
}
}
This same code works in 2.2 till 2.3
But not in the latest, 2.3.2
I get the server started prompt in the adb logcat
03-26 18:18:26.005 15056 15056 W Httpd : Web server initialized.
But all other requests returns Not Found
>$ curl -X GET http://192.168.1.2:8089
Not Found
>$ curl -X GET http://192.168.1.2:8089/demo.mp3
Not Found
I can't find what the problem is with the code?
Your serve() does not override any method NanoHTTPD is calling. The default implementation returns "404 Not Found".
The signature for serve() is
protected Response serve(IHTTPSession session)
However it's deprecated. Have a look at IHandlers as introduced in this commit. (The default handler does still call the deprecated serve() method.)

Kryonet RMI, cannot wait for response on connection's update thread

I would like to make a game using LibGDX and Kryonet library, using RMI. So I created clean project. What I want to do for now is, setup server to listen on port 10048 and on new connection to print client's name which will I get by calling a method on client's class...
Here is the code:
ICardsTableImpl.java
package clzola.cardstable.client;
public interface ICardsTableGameImpl {
public String getName();
}
CardsTableServer.java
package clzola.cardstable.server;
import clzola.cardstable.client.ICardsTableGameImpl;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;
import com.esotericsoftware.minlog.Log;
import java.io.IOException;
import java.util.HashMap;
public class CardsTableServer extends Server {
private HashMap<Integer, Connection> connections;
public CardsTableServer() throws IOException {
connections = new HashMap<Integer, Connection>();
addListener(new NetworkListener(this));
Kryo kryo = getKryo();
ObjectSpace.registerClasses(kryo);
kryo.register(ICardsTableGameImpl.class);
bind(10048);
}
#Override
protected Connection newConnection() {
Player player = new Player();
addConnection(player);
return player;
}
public void addConnection(Connection connection) {
this.connections.put(connection.getID(), connection);
}
public Connection getConnection(int connectionId) {
return this.connections.get(connectionId);
}
public Connection removeConnection(int connectionId) {
return this.connections.remove(connectionId);
}
public static void main(String[] args) throws IOException {
Log.set(Log.LEVEL_DEBUG);
CardsTableServer server = new CardsTableServer();
server.start();
}
}
NetworkListener.java
package clzola.cardstable.server;
import clzola.cardstable.client.ICardsTableGameImpl;
import com.badlogic.gdx.Gdx;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;
public class NetworkListener extends Listener {
private CardsTableServer server;
public NetworkListener(CardsTableServer server) {
this.server = server;
}
#Override
public void connected(Connection connection) {
Player player = ((Player) connection);
ICardsTableGameImpl game = ObjectSpace.getRemoteObject(player, 0, ICardsTableGameImpl.class);
player.name = game.getName(); // This is where I get excpetion...
Gdx.app.log("Server", "Player name: " + player.name);
}
#Override
public void disconnected(Connection connection) {
server.removeConnection(connection.getID());
}
}
Player.java
package clzola.cardstable.server;
import com.esotericsoftware.kryonet.Connection;
public class Player extends Connection {
public String name;
}
CardsTableGame.java
package clzola.cardstable.client;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;
public class CardsTableGame extends ApplicationAdapter implements ICardsTableGameImpl {
SpriteBatch batch;
Stage stage;
Client client;
String name = "Lazar";
ObjectSpace objectSpace;
#Override
public void create () {
batch = new SpriteBatch();
stage = new Stage(new ScreenViewport(), batch);
try {
client = new Client();
client.start();
Kryo kryo = client.getKryo();
ObjectSpace.registerClasses(kryo);
kryo.register(ICardsTableGameImpl.class);
ObjectSpace objectSpace = new ObjectSpace();
objectSpace.register(0, this);
objectSpace.addConnection(client);
client.connect(5000, "127.0.0.1", 10048);
} catch (Exception e) {
Gdx.app.log("CardsTableGame", e.getMessage(), e);
}
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public String getName() {
return this.name;
}
}
After running it, I get exception on the server side:
Exception in thread "Server" java.lang.IllegalStateException: Cannot wait for an RMI response on the connection's update thread.
at com.esotericsoftware.kryonet.rmi.ObjectSpace$RemoteInvocationHandler.waitForResponse(ObjectSpace.java:420)
at com.esotericsoftware.kryonet.rmi.ObjectSpace$RemoteInvocationHandler.invoke(ObjectSpace.java:408)
at com.sun.proxy.$Proxy0.getName(Unknown Source)
at clzola.cardstable.server.NetworkListener.connected(NetworkListener.java:24)
at com.esotericsoftware.kryonet.Server$1.connected(Server.java:48)
at com.esotericsoftware.kryonet.Connection.notifyConnected(Connection.java:214)
at com.esotericsoftware.kryonet.Server.acceptOperation(Server.java:417)
at com.esotericsoftware.kryonet.Server.update(Server.java:249)
at com.esotericsoftware.kryonet.Server.run(Server.java:372)
at java.lang.Thread.run(Thread.java:745)
And I have no idea why... What am I doing wrong??
(This is the first time ever I am trying to use RMI)
The Listener is executed by the Kryonet-update-thread. This thread is checking the socket regularly to receive the messages. Calling game.getName() makes the caller wait until the answer was delivered over the network. If you do that on the update thread you'd probably put your server in deadlock because kryonet cannot receive the answer it is waiting on, since you block the update thread. This is why it throws the exception.
In an rmi example from the kryonet git they solve this problem by using a Listener working on its own thread.
// The ThreadedListener means the network thread won't be blocked when waiting for RMI responses.
client.addListener(new ThreadedListener(new Listener() {
public void connected (final Connection connection) {
TestObject test = ObjectSpace.getRemoteObject(connection, 42, TestObject.class);
// Normal remote method call.
assertEquals(43.21f, test.other());
// Make a remote method call that returns another remote proxy object.
OtherObject otherObject = test.getOtherObject();
// Normal remote method call on the second object.
assertEquals(12.34f, otherObject.value());
// When a remote proxy object is sent, the other side recieves its actual remote object.
connection.sendTCP(otherObject);
}
}));

I cannot connect to my remote MySQL via Android SDK

I have two similar program. the First: Java for PC, the Second: Android. I have a MacOS Pro Server with ip 192.168.0.103 in my local network. MacOS Server has got MySQL-Server 5.0.1, with
CREATE DATABASE db_demo01;
CREATE USER 'user01'#'%' IDENTIFIED BY '1234567';
GRANT ALL ON db_demo01.* TO 'user01'#'%';
My PC App has following code:
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.0.103:3306/";
String dbName = "db_demo01";
String userName = "user01";
String userPass = "1234567";
try
{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url+dbName,userName,userPass);
System.out.println("Connected!");
conn.close();
}
catch(Exception ex)
{
System.out.println("Error:" + ex.getMessage());
}
It's work!But when I try to do this with my android app, I'v got connection fail. I don't know why... I posted the full code of my app below:
package com.navi.newser;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
/// VARIABLES
private TextView textWidget;
private TableLayout tableWidget;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CreateOnClickConnect();
}
private void CreateOnClickConnect()
{
textWidget = (TextView)findViewById(R.id.textView1);
tableWidget = (TableLayout)findViewById(R.id.table1);
Button cmd = (Button)this.findViewById(R.id.button3);
cmd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ConnectToMySQL();
}
});
}
private class Connect extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... urls)
{
String response = "";
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.0.103:3306/";
String dbName = "db_demo01";
String userName = "user01";
String userPass = "1234567";
Connection conn = null;
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,userPass);
response += "Connected!";
Log.e("MySQL", response);
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
response += "Error:" + ex.getMessage();
Log.e("MySQL", response);
}
publishProgress("Almost...");
return response;
}
#Override
protected void onPostExecute(String result) {
textWidget.setText(result);
}
#Override
protected void onProgressUpdate(String... text) {
textWidget.setText(text[0]);
}
}
public void ConnectToMySQL()
{
new Connect().execute();
}
I use Eclipse Luna.
01-20 19:09:07.777: E/dalvikvm(1069): Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
I don't have other errors.
The emulator is always isolated from your machine's network, take a look at this Network Address Space
Start your emulator with this
emulator -avd avdname -http-proxy http://192.168.0.1:8080
Here replace avdname to name of avd you want to run your program, and http:192.168.1.1 to your proxy server.
original answer
Well you have to make some settings; first of all with emulator isn't always a good idea to use it, while there is internet connection in the middle. Also the library (jar) for the connector should be in a folder called lib in Android project. For MySQL interaction, I use JSON with PHP. I hope this help you !

Categories