I'm making an application that using nodejs as a server on android device. Server will handle req and return html interface to desktop client via their web browser but i don't know what to start with Android studio.
-
2What's the question? – Alvin Abia Oct 20 '16 at 04:09
-
I don't know how to start, such as where to place nodejs code in android studio .... – Vincent Valentine Oct 20 '16 at 04:12
2 Answers
follow this link. this will help you get started. You need to install a terminal like termux which can act as a terminal to install all the required stuff. in node js is properly installed on your android device then you can do the request from desktop to your android device by knowing its IP address and server port
As per your comment you device is rooted then you can execute terminal commands via java. you need to grab the su standard input and run command this way taken from
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("Your command to start your nodejs server");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
throw new Exception(e);
}catch(InterruptedException e){
throw new Exception(e);
}

- 1
- 1

- 9,812
- 4
- 39
- 89
-
tks very much, i have visted your link, but having a way to start android server by code, without terminal ? – Vincent Valentine Oct 20 '16 at 04:43
-
-
I am guessing, that you want to use load html returned by server in the Android app. if that is the case, you can simply create an activity with a webview and load the same url you would load in the app.
You can follow this documentation https://developer.android.com/guide/webapps/webview.html to load url.
simply put, have a layout with webview
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
and in the activity load your servers url.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");
and if you want handle all links in that html to be opened in the same app follows this Clicking URLs opens default browser
But this is as good as somone opening the url in a browser in android phone, to give a better experience, you server should be able to give data as JSON or XML or any other data format and you should be able to build a native app on that data.
If this is not what you were looking for, apologies.