在nodejs伺服器和ABAP伺服器上使用jsonp
Suppose we have packaged our UI5 application into mobile platform and need to consume native API provided by mobile platform, and it is time for Cordova plugin to come on the stage. A Cordova plugin is a package of injected code that allows the Cordova webview within which the app renders to communicate with the native platform on which it runs.
In this blog, I still use the UI5 application mentioned in my previous blog Step by step to package a Fiori application into your Android device using Cordova for demo.
I will show the steps how to create a dummy plugin for Android platform, which simply performs the calculation between two integers implemented in Java, and then could be consumed by JavaScript code in UI5. There is indeed a page in Cordova website talking about how to create a new plugin, however I fail to create a plugin just simply by following it.
In my opinion some information is missing in the document, so it is the reason why I decide to document my detailed step here for future reference.
Note
The steps might vary with different version of Cordova. This blog is made based on version 7.0.1:
Detail steps to create plugin
(1) Install plugman via npm:
Then create a new plugin via the following command:
Once done, in the root folder of project there is a new folder generated, whose name is equal to the option specified via command -name:
Some artifacts are automatically generated and stored within the plugin folder. We don’t need to touch them at the moment.
(2) Since I need to develop a plugin used for Android platform, so enable the created plugin with Android platform via command: “plugman platform add –platform_name android”
Once done, a .java source file is created, which is used to implement the logic for example performing some native API call.
Copy the following source code to this Adder.java:
package jerry.adder;import org.apache.cordova.CordovaPlugin;import org.apache.cordova.CallbackContext;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class Adder extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("performAdd")) {
int arg1 = args.getInt(0);
int arg2 = args.getInt(1);
/* Indicating success is failure is done by calling the appropriate method on the
callbackContext.*/
int result = arg1 + arg2;
callbackContext.success("result calculated in Java: " + result);
return true;
}
return false;
}}
(3) Perform the following command to automatically generate a descriptor file, package.json for created plugin. Just press enter key again and again to simply use the default value, which are enough for this exercise.
Once done, the package.json file is generated within plugin folder.
(4) Install this plugin via command “cordova plugin add Adder”. If everything works fine, you should see message “BUILD SUCCESSFUL”.
Now just perform cordova compile, and the plugin will be built into the final APK file.
A list of artifacts generated for the created plugin
Let’s review what artifacts / configuration finally we have regarding this created plugin:
(1) in config.xml in the root folder, our plugin is added:
(2) The implementation Adder.java file for plugin in plugin folder.
(3) In the plugin folder there is a plugin.xml file, which defines the path the plugin implementation will be located in the platform specific folder.
In my project the target-dir points to this folder as below:
Consume the plugin in UI5 application
In order to test this plugin, paste the following source code to index.js in path: /platforms/android/assets/www/js:
var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler // // Bind any cordova events here. Common events are: // 'pause', 'resume', etc. onDeviceReady: function() {
this.receivedEvent('deviceready');
},
receivedEvent: function(id) {
function success(result){
debugger;
alert("Jerry plugin result: " + result);
};
setTimeout( function(){
debugger;
Cordova.exec(success, null, "Adder", "performAdd", [10,20]);
}, 10000);
}};app.initialize();
The consumption is done in line 38, here I use a 10 seconds delay just in order to ease my debugging. ( Java Plugin debugging is worth another blog )
Execute the application in my mobile phone, the plugin will be executed in Java layer and result is passed back to my UI5 application:
If you would like to know how JavaScript code is passed to Java code, please read this blog: How is JavaScript code in OData offline plugin delegated to native Java code in Android.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2717097/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在 SAPGUI 裡使用 ABAP 報表上傳 SAP UI5 應用到 ABAP 伺服器試讀版GUI伺服器
- 在伺服器上使用 smart http 搭建 Git 伺服器伺服器HTTPGit
- 如何使用 JavaScript 程式碼連線部署在 SAP ABAP 伺服器上的 OData 服務JavaScript伺服器
- 使用nodejs和Java訪問遠端伺服器的服務NodeJSJava伺服器
- [譯] 使用 NodeJS 建立一個 GraphQL 伺服器NodeJS伺服器
- nodejs搭建web伺服器NodeJSWeb伺服器
- nodejs web伺服器建立NodeJSWeb伺服器
- 使用 JavaScript 上傳 PDF 和 Excel 等二進位制檔案到 ABAP 伺服器並進行解析JavaScriptExcel伺服器
- 在CentOS上搭建git伺服器CentOSGit伺服器
- 在Pycharm上使用遠端伺服器進行除錯PyCharm伺服器除錯
- 不使用任何框架,手寫純 JavaScript 實現上傳本地檔案到 ABAP 伺服器框架JavaScript伺服器
- 使用libvirt 在伺服器上安裝openSuse虛擬機器伺服器虛擬機
- 使用openSSL構造一個支援https的nodejs伺服器HTTPNodeJS伺服器
- 在 Fedora 上安裝 MongoDB 伺服器MongoDB伺服器
- 瀏覽器與伺服器通訊技術——jsonp瀏覽器伺服器JSON
- 搭建 nodeJS 伺服器之(2)sequelizeNodeJS伺服器
- 使用Java程式通過http post訪問ABAP Netweaver伺服器JavaHTTP伺服器
- 在Nodejs下與Rabbitmq訊息伺服器通訊(node-amqp)NodeJSMQ伺服器
- 使用 ABAP 程式語言直接執行 ABAP 伺服器所在作業系統的 shell 命令伺服器作業系統
- 在滴滴雲 DC2 雲伺服器上使用 LVM 管理磁碟伺服器LVM
- 在Visual Studio 中使用git——同步到遠端伺服器-上(十一)Git伺服器
- 在 Linux 上配置一個 syslog 伺服器Linux伺服器
- 在Mac上搭建自己的伺服器——NginxMac伺服器Nginx
- 在 Android 裝置上搭建 Web 伺服器AndroidWeb伺服器
- 在 Windows 上搭建 MQTT 訊息伺服器WindowsMQQT伺服器
- nodejs中express搭建本地web伺服器NodeJSExpressWeb伺服器
- 在Thymeleaf和HTMX中使用伺服器傳送的事件 - Wim伺服器事件
- 如何在伺服器上使用 Tensorflow, Notebook伺服器
- 如何在linux伺服器上使用hanlpLinux伺服器HanLP
- Linux伺服器上使用docter安裝指定版本的nginx伺服器Linux伺服器Nginx
- 在linux centos伺服器上安裝telnet命令LinuxCentOS伺服器
- 在linux伺服器上搭建slurm叢集部署Linux伺服器
- Linux centos7上gitlab伺服器的搭建,本地配置和使用LinuxCentOSGitlab伺服器
- nodejs平臺內建模組http伺服器NodeJSHTTP伺服器
- 本地開發好的 SAP Fiori Elements 應用,如何部署到 ABAP 伺服器上?伺服器
- linux 伺服器上 jmeter 的搭建及使用Linux伺服器JMeter
- 使用Dnsmasq搭建本地dns伺服器上網DNS伺服器
- expressjs路由和Nodejs伺服器端傳送REST請求 - - ITeye部落格Express路由NodeJS伺服器REST