Android rmi 框架,Using JAVA RMI in Android application

该示例展示了如何通过LipeRMI库创建一个Android客户端与Java服务器进行交互。在Java应用中定义了一个接口`TestService`和实现了该接口的`TestServer`类,服务器监听7777端口。Android客户端则通过`AsyncTask`调用`TestService`的方法获取响应。确保在两个项目中都添加了LipeRMI库,并且Android项目中添加了INTERNET权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 {

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值