把 android 手机变成 web server (golang)
配置 golang 开发环境
略
安装并初始化 gomobile
go get golang.org/x/mobile/cmd/gomobile
gomobile init
创建 beego 项目, 参考 https://beego.me/quickstart
cd $GOPATH/src
bee new androidweb
# 创建 gomobile 文件
cd androidweb
mkdir androidweb
gedit androidweb.go
androidweb.go 的内容
package androidweb
import (
"androidweb/controllers"
_ "androidweb/routers"
"github.com/astaxie/beego"
)
func Run(dir string) {
beego.Router("/", &controllers.MainController{})
beego.LoadAppConfig("ini", dir+"/conf/app.conf")
beego.SetStaticPath("/static", dir+"/static")
beego.BConfig.WebConfig.ViewsPath = dir + "/views"
beego.Run()
}
生成 aar,
cd $GOPATH/src
gomobile bind -target=android androidweb/androidweb/
成功的话,会在 $GOPATH/src 目录下创建一个 androidweb.aar 文件
创建 android 项目,并添加权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
将 beego 项目中的 conf static views 复制到 android 项目的 assets 中
导入前面生成的 androidweb.aar 文件
修改 MainActivity
import android.content.Context;
import android.content.res.AssetManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import androidweb.Androidweb;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView) findViewById(R.id.textView)).setText("启动成功..." + "\n" +
"IP: " + getIpAddress() + "\n" +
appConf());
copyFolder("conf");
copyFolder("static");
copyFolder("views");
new Thread() {
@Override
public void run() {
super.run();
Androidweb.run(getFilesDir().getAbsolutePath());
}
}.start();
}
private String appConf() {
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(getAssets().open("conf/app.conf")));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
sb.append(mLine + "\n");
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
return sb.toString();
}
private String getIpAddress() {
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wm.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
WifiInfo info = wm.getConnectionInfo();
int hostip = info.getIpAddress();
String ip = (hostip & 0xFF) + "." + ((hostip >> 8) & 0xFF) + "." + ((hostip >> 16) & 0xFF) + "." + ((hostip >> 24) & 0xFF);
return ip;
}
return null;
}
private void copyFolder(String path) {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File dir = new File(getFilesDir() + "/" + path);
if (!dir.exists()) {
dir.mkdirs();
}
try {
String[] files = assetManager.list(path);
for (String filename : files) {
in = assetManager.open(path + "/" + filename);
File outFile = new File(dir, filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
}
启动 android 程序即可


本文来自:开源中国博客
把 android 手机变成 web server (golang)的更多相关文章
- Android手机用KSWEB搭建Web服务器成功安装WordPress
之前部落分享的几个免费Web服务器软件都是用来安装在本地电脑上,搭建Apache.PhpMyAdmin.MySQL等网站运行环境,然后我们就可以在电脑上测试运行Wordpress.Discuz! 论坛 ...
- Creating A Simple Web Server With Golang
原文:https://tutorialedge.net/post/golang/creating-simple-web-server-with-golang/ -------------------- ...
- Android手机流量分析工具介绍
一.20 Best Android Hacking Apps And Tools Of 2018 首先罗列常见的Android手机hacking的工具 #1The Android Network Ha ...
- Fiddler抓取Android手机网络数据包
也许你也有疑问,如果开发没有完备的接口文档,我如何知道他API的信息? 这时我们就得通过一些抓包工具抓取这些API信息. 常见抓包工具 HTTP抓包工具:Fiddler.Charles.Firebug ...
- Pyqt adb 获取Android手机屏幕
adb的全称为Android Debug Bridge,就是起到调试桥的作用.adb的工作方式比较特殊,采用监听Socket TCP 5554等端口的方式让IDE和Qemu通讯,默认情况下adb会da ...
- Android手机浏览器访问本地网络相关问题
为了测试开发的手机网站,常常需要使手机直接访问本地网络. 在这个过程中碰到几个问题,记下来供以后参考 1. 在本地主机运行apache后,使用localhost和127.0.0.1可以访问页面,但使用 ...
- 使用Android应用调用Web Service
Java本身提供了丰富的Web Service支持,比如Sun公司指定的JAX-WS 2规范,还有Apache开源组织所提供的Axis1.Axis2.CXF等,这些技术不仅可以用于非常方便地对外提 ...
- mac 下真机调试 android 手机
第一步: 查看usb设备信息 在 终端输入:system_profiler SPUSBDataType 可以查看连接的usb设备的信息 比如我的usb信息如下(部分内容): Spreadtru ...
- 手机H5 web调试利器——WEINRE (WEb INspector REmote)
手机H5 web调试利器--WEINRE (WEb INspector REmote) 调试移动端页面,优先选择使用chrome浏览器调试,如果是hybrid形式的页面,可以使用chrome提供的ch ...
随机推荐
- Java 数据库简单操作类
数据库操作类,将所有连接数据库的配置信息以及基本的CRUD操作封装在一个类里,方便项目里使用,将连接数据库的基本信息放在配置文件 "dbinfo.properties" 中,通过类 ...
- Thread(生产者和消费者) wait、notify、notifyAll
在java中,线程间的通信可以使用wait.notify.notifyAll来进行控制.从名字就可以看出来这3个方法都是跟多线程相关的,但是可能让你感到吃惊的是:这3个方法并不是Thread类或者是R ...
- Postman的Post请求方式的四种类型的数据
1. form-data 就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件时 ...
- Laravel5.5+ 区分前后端用户登录
Laravel 的用户认证是通过 Auth Facade 门脸实现的,手动认证可是使用 Auth::login() 或 Auth::attempt() 这两个方法实现. 以下内容纯属个人实现,也许有 ...
- 过滤数组中的空数组array_filter()
手册查询array_filter()的使用细节时,看到了:If no callback is supplied, all entries of input equal to FALSE (see co ...
- 二叉搜索树的第k个节点
给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. = =一看就想到中序遍历 public class Soluti ...
- Centos6.8 安装git
1.下载安装包 wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.8.0.tar.gz 2.安装依赖 sudo yum - ...
- python数学第八天【协方差】
- 错误模块名称: KERNELBASE.dll
部署win服务时,经常会出现类似下面的错误,错误模块名称: KERNELBASE.dll. 日志名称: Application 来源: Application ...
- vs code安装
vs code是一款文本编辑器,开源,是前端界的vs,而Dreamweaver适合入门. user版本的一些系统分区文件夹无法创建,可能存在语言显示问题.一般用户建议使用system版. 下载链接:h ...