Skip to content

http相关

javascript
const core = require('cheese-js');
lua
local core = require('cheese-lua')

get请求:public get(): http

用法示例:

javascript
const core = require('cheese-js');
const http = new core.http;
let body = http.builder().url("https://www.baidu.com").get().sync()
console.log(body)
lua
local core = require('cheese-lua')
local http = core.http.new()
local body = http:builder():url("https://www.baidu.com"):get():sync()
print(body)

get请求:public post: http

用法示例:

javascript
const core = require('cheese-js');
const http = new core.http;
let body = http.builder().url("https://www.baidu.com").post().sync()
console.log(body)
lua
local core = require('cheese-lua')
local http = core.http.new()
local body = http:builder():url("https://www.baidu.com"):post():sync()
print(body)

文件下载:public download(url, targetPath, callback): boolean

用于将指定 url 的文件下载到本地 targetPath,可通过 callback 接收下载进度回调。

参数说明:

  • url: 下载地址
  • targetPath: 本地保存路径
  • callback: 下载进度回调(可选),签名为 (downloadedBytes, totalBytes, progress)

回调参数说明:

  • downloadedBytes: 当前已下载字节数
  • totalBytes: 文件总字节数(未知时可能为 -1
  • progress: 百分比进度(0~100,总大小未知时为 -1

用法示例:

javascript
const core = require("cheese-js");
const http = new core.http();

const ok = http.download(
  "https://example.com/file.zip",
  "/sdcard/Download/file.zip",
  (downloadedBytes, totalBytes, progress) => {
    console.log("downloadedBytes:", downloadedBytes);
    console.log("totalBytes:", totalBytes);
    console.log("progress:", progress);
  }
);

console.log("download started:", ok);
lua
local core = require('cheese-lua')
local http = core.http.new()

local ok = http:download(
    "https://example.com/file.zip",
    "/sdcard/Download/file.zip",
    function(self,downloadedBytes, totalBytes, progress)
        print("downloadedBytes:", downloadedBytes)
        print("totalBytes:", totalBytes)
        print("progress:", progress)
    end
)

print("download started:", ok)

图鉴打码示例:

javascript
const core = require("cheese-js");
let http = new core.http()
let converters =  core.converters
let builder =http.builder()
builder.url("http://api.ttshitu.com/predict")
builder.addParam("username","333")
builder.addParam("password","333")
builder.addParam("typeid","1001")
builder.addParam("image",converters.bitmapToBase64(converters.assetsToBitmap("img.png") ))
builder.post(false)
console.log(builder.sync())
lua
local core = require('cheese-lua')
local http = core.http.new()
local converters = core.converters
local builder = http:builder()
builder:url("http://api.ttshitu.com/predict")
builder:addParam("username","333")
builder:addParam("password","333")
builder:addParam("typeid","1001")
builder:addParam("image", converters.bitmapToBase64(converters.assetsToBitmap("img.png")))
builder:post(false)
print(builder:sync())

Released under the GPL-3.0 License.