1、創(chuàng)建cordova項目
- 創(chuàng)建項目
cordova create nano-test com.liang.nano nanoTest
- 添加android平臺
cordova platforms add android
2帮匾、添加NanoHTTPD
{項目目錄}\nano-test\platforms\android\app\build.gradle
dependencies{} 內(nèi)添加
//nanohttpd
implementation 'org.nanohttpd:nanohttpd:2.3.1'
3、實現(xiàn)服務(wù)
新建HttpServer 繼承 NanoHTTPD 躏率,serve(IHTTPSession session)方法為所有請求的入口
package com.liang.nano;
import android.content.Context;
import org.apache.cordova.LOG;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import fi.iki.elonen.NanoHTTPD;
public class HttpServer extends NanoHTTPD {
public static String TAG = "HttpServer";
private Context mContext;
public HttpServer(int port) {
super(port);
}
public HttpServer(int port,Context mContext) {
super(port);
this.mContext = mContext;
}
// 所有請求的入口
@Override
public Response serve(IHTTPSession session) {
int code = 1;
String msg = "success";
String uri = session.getUri();
String method = session.getMethod().toString();
String queryParameterString = session.getQueryParameterString();
Map<String,String> body = new HashMap<>();
try {
session.parseBody(body);
} catch (IOException | ResponseException e) {
code = 0;
msg = "body to JSON fail";
LOG.e(TAG,msg,e);
}
Map<String,Object> result = new HashMap<>();
result.put("code",code);
result.put("msg",msg);
result.put("body",body);
result.put("uri",uri);
result.put("method",method);
result.put("queryParameterString",queryParameterString);
JSONObject jsonObject = new JSONObject(result);
return newFixedLengthResponse(Response.Status.OK,"application/json",jsonObject.toString());
}
}
4瘫寝、修改cordova的MainActivity
增加/* HttpServer start /與/ HttpServer end */之間的內(nèi)容
package com.liang.nano;
import android.os.Bundle;
import org.apache.cordova.*;
import java.io.IOException;
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
/* HttpServer start */
try {
// 監(jiān)聽端口9100
HttpServer httpServer = new HttpServer(9100,this);
// 服務(wù)啟動
httpServer.start();
} catch (IOException e) {
LOG.e(TAG,"HttpServer start fail",e);
}
/* HttpServer end */
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}
5.編譯打包成apk并安裝
6.測試
1.手機(jī)測試
- 啟動APP
- 不要關(guān)閉APP,切換到后臺仅讽,打開手機(jī)瀏覽器
- 瀏覽器地址欄訪問
http://127.0.0.1:9100/api/test?userId=sylmj
- 瀏覽器頁面得到響應(yīng)
["body":{},"msg":"success","uri":"\/api\/test","queryParameterString":null,"code":1}
2.安卓模擬器測試
問題處理
1)android studio識別不到模擬器設(shè)備
- 啟動Android Studio
- 啟動夜神模擬器
- 找到自己Android Studio使用的SDK對應(yīng)的文件夾,找到platform-tools文件夾钾挟,比如我的在D:\SDK\platform-tools洁灵。
- 在這個目錄下啟動cmd命令窗口,執(zhí)行下面命令
夜神模擬器:
adb connect 127.0.0.1:62001
mumu模擬器:
adb connect 127.0.0.1:7555
2)獲取局域網(wǎng)的ip地址
我使用的是夜神模擬器掺出,需要將網(wǎng)絡(luò)設(shè)置改成橋接模式
系統(tǒng)設(shè)置-手機(jī)-網(wǎng)絡(luò)設(shè)置-勾選 橋接模式-IP模式 勾選DHCP
上面顯示的IP地址即為模擬器的局域網(wǎng)IP地址徽千,我的為192.168.0.101
測試
- 瀏覽器地址欄訪問
http://192.168.0.101:9100/api/test?userId=sylmj
- 瀏覽器頁面得到響應(yīng),與手機(jī)本機(jī)測試效果一致
["body":{},"msg":"success","uri":"\/api\/test","queryParameterString":null,"code":1}
postman發(fā)送POST請求
地址
http://192.168.0.101:9100/api/test?userId=sylmj
- 請求體
{
"zjhm":"123"
}
- 響應(yīng)
{
"body": {
"postData": "{\r\n \"zjhm\":\"123\"\r\n}"
},
"method": "POST",
"msg": "success",
"uri": "/api/test",
"queryParameterString": "userId=sylmj",
"code": 1
}