HTTP 网络封装
官方http封装
get httpGet封装
| 参数名 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| url | 是 | string | 请求链接 | 
| parms | 否 | Object | 请求参数  {"a":1} | 
| dataType | 否 | String | 默认json string/json | 
| timeout | 否 | int | 超时时间 秒 默认10 | 
| headers | 否 | Object | 请求头信息 {"a":1} | 
let url = "http://www.baidu.com",
    parms = {data: "abc"},
    dataType = "string",
    timeout = 30,
    headers = {UA: "xxxxx"},
    res = null
//默认返回值为Object
res = laoleng.http.get(url)
if (res) {
    logd(JSON.stringify(res))
    logd(res.code)
}
logd(laoleng.http.get(url, parms));
//指定返回值为string
ret = laoleng.http.get(url, parms, dataType)
if (ret) {
    logd(ret)
}
logd(laoleng.http.get(url, parms, dataType, timeout));
//单独指定超时时间
res = laoleng.http.get(url, null, null, timeout)
if (res) {
    logd(JSON.stringify(res))
    logd(res.code)
}
logd(laoleng.http.get(url, parms, dataType, timeout, headers));
| 返回值 | 类型 | 说明 | 
|---|---|---|
| 默认Object/字符串 | Object/string | 返回信息 | 
post httpPost封装
| 参数名 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| url | 是 | string | 请求链接 | 
| parms | 否 | Object | 请求参数  {"a":1} | 
| dataType | 否 | String | 默认json string/json | 
| timeout | 否 | int | 超时时间 秒 默认10 | 
| headers | 否 | Object | 请求头信息 {"a":1} | 
let url = "http://www.baidu.com",
    parms = {"data1": "abc", "data2": 123},
    dataType = "string",
    timeout = 30,
    headers = {UA: "xxxxx"},
    res = null
//默认返回值为Object
res = laoleng.http.post(url)
if (res) {
    logd(JSON.stringify(res))
    logd(res.code)
}
logd(laoleng.http.post(url, parms));
//指定返回值为string
ret = laoleng.http.post(url, parms, dataType)
if (ret) {
    logd(ret)
}
logd(laoleng.http.post(url, parms, dataType, timeout));
//单独指定超时时间
res = laoleng.http.post(url, null, null, timeout)
if (res) {
    logd(JSON.stringify(res))
    logd(res.code)
}
logd(laoleng.http.get(url, parms, dataType, timeout, headers));
| 返回值 | 类型 | 说明 | 
|---|---|---|
| 默认Object/字符串 | Object/string | 返回信息 | 
postJson postJson封装
| 参数名 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| url | 是 | string | 请求链接 | 
| parms | 否 | Object | 请求参数  {"a":1} | 
| dataType | 否 | String | 默认json string/json | 
| timeout | 否 | int | 超时时间 秒 默认10 | 
| headers | 否 | Object | 请求头信息 {"a":1} | 
let url = "http://www.baidu.com",
    parms = {data: "abc"},
    dataType = "string",
    timeout = 30,
    headers = {UA: "xxxxx"},
    res = null
//默认返回值为Object
res = laoleng.http.postJson(url)
if (res) {
    logd(JSON.stringify(res))
    logd(res.code)
}
logd(laoleng.http.postJson(url, parms));
//指定返回值为string
ret = laoleng.http.postJson(url, parms, dataType)
if (ret) {
    logd(ret)
}
logd(laoleng.http.postJson(url, parms, dataType, timeout));
//单独指定超时时间
res = laoleng.http.postJson(url, null, null, timeout)
if (res) {
    logd(JSON.stringify(res))
    logd(res.code)
}
logd(laoleng.http.get(url, parms, dataType, timeout, headers));
| 返回值 | 类型 | 说明 | 
|---|---|---|
| 默认Object/字符串 | Object/string | 返回信息 | 
Jsoup
jsoup.get 主要用来下载bytes流
| 参数名 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| url | 是 | string | 请求链接 | 
| timeout | 否 | int | 超时时间 秒 默认30 | 
| retType | 否 | String | 返回值格式 string/byte/json/stream 默认string | 
//指定返回值为byte
let ret = laoleng.jsoup.get("http://www.baidu.com", 100, "byte")
if (ret) {
    laoleng.files.writeFileBytes("/sdcard/1.png", res.bytes)
}
//指定返回值为stream流
let ret = laoleng.jsoup.get("http://www.baidu.com", 100, "stream")
if (ret) {
    laoleng.files.writeFileBytes("/sdcard/1.png", res)
}
//默认返回值为String
logd(laoleng.jsoup.get("http://www.baidu.com"));
//指定超时
logd(laoleng.jsoup.get("http://www.baidu.com", 5));
//指定返回值为json
let res = laoleng.jsoup.get("http://www.baidu.com", 10, "json")
if (res) {
    logd(JSON.stringify(res))
    logd(res.code)
}
| 返回值 | 类型 | 说明 | 
|---|---|---|
| 默认Object/字符串 | Object/string | 返回信息 | 
jsoup.post 主要用来直接请求data数据
| 参数名 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| url | 是 | string | 请求链接 | 
| data | 是 | string | 请求数据 | 
| timeout | 否 | number | 超时,默认30s | 
| retType | 否 | string | 返回值格式 string/byte/json/stream 默认string | 
logd(laoleng.jsoup.post("http://www.baidu.com","123"))
| 返回值 | 类型 | 说明 | 
|---|---|---|
| 网页返回 | any | 网页返回 | 
jsoup.put 主要用来请求put数据
| 参数名 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| url | 是 | string | 请求链接 | 
| data | 否 | string/Object | 请求数据 | 
| header | 否 | Object | 请求头 | 
| number | 否 | number | 超时,默认30s | 
| String | 否 | string | 返回值格式 string/byte/json/stream 默认json | 
//直接请求
let res = laoleng.jsoup.put("http://www.baidu.com")
logd(JSON.stringify(res))
//提交对象data
let res = laoleng.jsoup.put("http://www.baidu.com", {a: 1, b: 2})
logd(JSON.stringify(res))
//提交拼接data
let res = laoleng.jsoup.put("http://www.baidu.com", "a=1&b=2")
logd(JSON.stringify(res))
//提交json数据,需设置header
let data = {a: 1, b: 2}
let res = laoleng.jsoup.put("http://www.baidu.com", JSON.stringify(data),
    {
        "Content-Type": "application/json"
    })
logd(JSON.stringify(res))
//提交json数据,需设置header,带超时
let data = {a: 1, b: 2}
let res = laoleng.jsoup.put("http://www.baidu.com", JSON.stringify(data),
    {
        "Content-Type": "application/json"
    }, 60)
logd(JSON.stringify(res))
//提交json数据,需设置header,带超时,带返回格式string字符串
let data = {a: 1, b: 2}
let res = laoleng.jsoup.put("http://www.baidu.com", JSON.stringify(data),
    {
        "Content-Type": "application/json"
    }, 60, "string")
logd(res)
| 返回值 | 类型 | 说明 | 
|---|---|---|
{code:0} | any | 是否成功 | 
其他
getIpLocation 获取当前IP的地址
| 参数名 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| 无 | 无 | 无 | 无 | 
logd(laoleng.http.getIpLocation())
| 返回值 | 类型 | 说明 | 
|---|---|---|
| 福建省三明市 电信 | string | 当前ip定位地址 | 
getNetTimeStamp 获取10位网络时间戳
| 参数名 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| 无 | 无 | 无 | 无 | 
logd(laoleng.http.getNetTimeStamp());
| 返回值 | 类型 | 说明 | 
|---|---|---|
| 1616484744 | long | 10位网络时间戳 |