登录
原创

基于JAVA的天聚人合流量接口调用示例代码

专栏天聚人合TIANJURENHE.COM
发布于 2020-12-21 阅读 940
  • Java
  • API
原创

前期准备

接口说明

  • 免费使用,根据会员等级不同,每日可调用次数不同,具体登录天聚人合官网查看
  • 话费API接口技术接入服务。为企业提供话费API接口,通过接口自由实现PC端、H5、APP等方式接入。
  • 其他更详细的接口使用信息,登录天聚人合官网查看,本文为简单使用示例,仅供参考

流量–查询全部流量套餐接口:

请求参数

名称 必填 类型 说明
key String 在个人中心->我的数据,接口名称上方查看

代码示例

import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

public class tjrh_ll{
    private static OkHttpClient client;
    /**查询全部流量套餐接口地址*/
    private static final String apiUrl_list = "http://op.tianjurenhe.com/flow/list";
    /**查询全部流量套餐接口参数格式*/
        public static final String LL_LIST_PARAM = "?key={0}";
    /**申请的接口Appkey*/
    public static final String key = 您申请的key;

    private static final int TIME_OUT = 20;

    static {
        client = new OkHttpClient.Builder()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }

 /**
     * 查询全部流量套餐
     * @return 返回String结果
     * @throws Exception
     */
    public static void findlist() throws IOException {
        //拼接地址与参数
        String url = apiUrl_list + MessageFormat.format(LL_LIST_PARAM,key);
        //创建request对象实例
        Request request = new Request.Builder().url(url).build();
        //获取请求response应答
        Response response = client.newCall(request).execute();
        if(response != null){
            //返回消息体的字符串
            JSONObject result = JSONObject.parseObject(response.body().string());
            if (result.getInteger("error_code") == 0) {
                // 订单提交成功,根据实际业务逻辑进行处理
            }else{
                // 提交返回码error_code非正常状态,依据官方文档错误码说明,进行逻辑处理
                // 比如:10014,系统异常 / 208516,重复的订单号 等,需要进行二次查询/人工确认处理,不要直接失败处理,避免造成不必要的损失
            }
        }else{
            // 可能网络异常等问题,未获得正确响应结果,建议进行二次查单/人工确认,不要直接失败处理,避免造成不必要的损失
            // 依据自己的业务逻辑进行处理
        }
    }


返回结果示例

{
   {
    "reason": "success",
    "result": [
        {
            "city": "全国",
            "company": "中国联通",
            "companytype": "1",
            "name": "中国联通全国流量套餐",
            "type": "1",
            "flows": [
                {
                    "id": "1",
                    "p": "50M",
                    "v": "50",
                    "inprice": "5.70"
                },
                {
                    "id": "2",
                    "p": "200M",
                    "v": "200",
                    "inprice": "14.25"
                },
                {
                    "id": "34",
                    "p": "20M",
                    "v": "20",
                    "inprice": "2.85"
                },
                {
                    "id": "35",
                    "p": "100M",
                    "v": "100",
                    "inprice": "9.50"
                },
                {
                    "id": "36",
                    "p": "500M",
                    "v": "500",
                    "inprice": "28.50"
                }
            ]
        },
        {
            "city": "全国",
            "company": "中国移动",
            "companytype": "2",
            "name": "中国移动全国流量套餐",
            "type": "1",
            "flows": [
                {
                    "id": "3",
                    "p": "10M",
                    "v": "10",
                    "inprice": "2.25"
                },
                {
                    "id": "4",
                    "p": "30M",
                    "v": "30",
                    "inprice": "3.75"
                },
                {
                    "id": "5",
                    "p": "70M",
                    "v": "70",
                    "inprice": "7.50"
                },
                {
                    "id": "7",
                    "p": "500M",
                    "v": "500",
                    "inprice": "22.50"
                },
                {
                    "id": "26",
                    "p": "1G",
                    "v": "1024",
                    "inprice": "37.50"
                },
                {
                    "id": "27",
                    "p": "2048M",
                    "v": "2048",
                    "inprice": "52.50"
                }
            ]
        },
        {
            "city": "全国",
            "company": "中国电信",
            "companytype": "3",
            "name": "中国电信全国流量套餐",
            "type": "1",
            "flows": [
                {
                    "id": "8",
                    "p": "10M",
                    "v": "10",
                    "inprice": "1.80"
                },
                {
                    "id": "9",
                    "p": "30M",
                    "v": "30",
                    "inprice": "4.50"
                },
                {
                    "id": "10",
                    "p": "100M",
                    "v": "100",
                    "inprice": "9.00"
                },
                {
                    "id": "11",
                    "p": "200M",
                    "v": "200",
                    "inprice": "13.50"
                },
                {
                    "id": "12",
                    "p": "500M",
                    "v": "500",
                    "inprice": "27.00"
                },
                {
                    "id": "28",
                    "p": "1G",
                    "v": "1024",
                    "inprice": "45.00"
                },
                {
                    "id": "32",
                    "p": "50M",
                    "v": "50",
                    "inprice": "6.30"
                }
            ]
        }
    ],
    "error_code": 0
}
}

流量----检测号码支持的流量套餐:

请求参数

名称 必填 类型 说明
key String 在个人中心->我的数据,接口名称上方查看
phone String 要查询的手机号码

代码示例

import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

public class tjrh_ll {
    private static OkHttpClient client;
    /**检测号码支持的流量套餐接口地址*/
    private static final String apiUrl_telcheck = "http://op.tianjurenhe.com/flow/telcheck";;
    /**流量充值接口参数格式*/
    public static final String LL_TELCHECK_PARAM = "?phone={0}&key={1}";
    /**申请的接口Appkey*/
    public static final String key = 您申请的key;

    private static final int TIME_OUT = 20;

    static {
        client = new OkHttpClient.Builder()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }

   /**
     * 检测号码支持的流量套餐
     * @return 返回String结果
     * @throws Exception
     */
    public static void telcheck(String phone) throws IOException {
        //拼接地址与参数
        String url = apiUrl_telcheck + MessageFormat.format(LL_TELCHECK_PARAM,phone,key);
        //创建request对象实例
        Request request = new Request.Builder().url(url).build();
        //获取请求response应答
        Response response = client.newCall(request).execute();
        if(response != null){
            //返回消息体的字符串
            JSONObject result = JSONObject.parseObject(response.body().string());
            if (result.getInteger("error_code") == 0) {
                System.out.println(result);
                // 订单提交成功,根据实际业务逻辑进行处理
            }else{
                // 提交返回码error_code非正常状态,依据官方文档错误码说明,进行逻辑处理
                // 比如:10014,系统异常 / 208516,重复的订单号 等,需要进行二次查询/人工确认处理,不要直接失败处理,避免造成不必要的损失
            }
        }else{
            // 可能网络异常等问题,未获得正确响应结果,建议进行二次查单/人工确认,不要直接失败处理,避免造成不必要的损失
            // 依据自己的业务逻辑进行处理
        }
    }

返回结果示例

{
    "reason": "success",
    "result": [
        {
            "city": "全国",
            "company": "中国移动",
            "companytype": "2",
            "name": "中国移动全国流量套餐",
            "type": "1",
            "flows": [
                {
                    "id": "3",
                    "p": "10M",
                    "v": "10",
                    "inprice": "2.90"
                },
                {
                    "id": "4",
                    "p": "30M",
                    "v": "30",
                    "inprice": "4.84"
                },
                {
                    "id": "5",
                    "p": "70M",
                    "v": "70",
                    "inprice": "9.50"
                },
                {
                    "id": "6",
                    "p": "150M",
                    "v": "150",
                    "inprice": "19.00"
                },
                {
                    "id": "7",
                    "p": "500M",
                    "v": "500",
                    "inprice": "29.04"
                },
                {
                    "id": "26",
                    "p": "1G",
                    "v": "1024",
                    "inprice": "48.40"
                },
                {
                    "id": "27",
                    "p": "2048M",
                    "v": "2048",
                    "inprice": "66.50"
                },
                {
                    "id": "33",
                    "p": "700M",
                    "v": "700",
                    "inprice": "38.72"
                }
            ]
        }
    ],
    "error_code": 0
}

流量—提交流量充值:

请求参数

名称 必填 类型 说明
phone String 需要充值流量的手机号码
pid String 流量套餐ID
orderid String 自定义订单号,8-32字母数字组合
key String 在个人中心->我的数据,接口名称上方查看
sign String 校验值,md5( OpenID+key+phone+pid+orderid),结果转为小写

代码示例

//MD5工具类
package com.tjrh.HttpRequest;

import java.security.MessageDigest;

public class MD5Util {

    public static String MD5(String s) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

        try {
            byte[] e = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(e);
            byte[] md = mdInst.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;

            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 15];
                str[k++] = hexDigits[byte0 & 15];
            }

            return new String(str).toLowerCase();
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
    }
}

//调用接口的主类
package com.tjrh.HttpRequest;

import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

public class tjrh_ll {
    private static OkHttpClient client;
    /**流量充值接口地址*/
    private static final String apiUrl_recharge = "http://op.tianjurenhe.com/flow/recharge";
    /**流量充值接口参数格式*/
    public static final String LL_RECHARGE_PARAM = "?key={0}&phone={1}&pid={2}&orderid={3}&sign={4}";
    /**在个人中心查询*/
    public static final String openId = 您的openid;
    /**申请的接口Appkey*/
    public static final String key = 您申请的key;
    /**在个人中心查询*/
    public static final String openId = 您的openId;

    private static final int TIME_OUT = 20;

    static {
        client = new OkHttpClient.Builder()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }

   /**
     * 依据用户提供的请求为指定油卡直接充值
     * @param phone 需要充值流量的手机号码
     * @param pid 流量套餐ID
     * @param orderid 自定义订单号,8-32字母数字组合
     * @return 返回String结果
     * @throws Exception
     */
    public static void recharge(String phone, String pid, String orderid) throws IOException {
        //OpenID+key+phone+pid+orderid
        String sign = MD5Util.MD5(openId + key + phone + pid + orderid);
        //拼接地址与参数
        String url = apiUrl_recharge + MessageFormat.format(LL_RECHARGE_PARAM, key, phone, pid, orderid , sign);
        //创建request对象实例
        Request request = new Request.Builder().url(url).build();
        //获取请求response应答
        Response response = client.newCall(request).execute();
        if(response != null){
            //返回消息体的字符串
            JSONObject result = JSONObject.parseObject(response.body().string());
            if (result.getInteger("error_code") == 0) {
                // 订单提交成功,根据实际业务逻辑进行处理
            }else{
                // 提交返回码error_code非正常状态,依据官方文档错误码说明,进行逻辑处理
                // 比如:10014,系统异常 / 208516,重复的订单号 等,需要进行二次查询/人工确认处理,不要直接失败处理,避免造成不必要的损失
            }
        }else{
            // 可能网络异常等问题,未获得正确响应结果,建议进行二次查单/人工确认,不要直接失败处理,避免造成不必要的损失
            // 依据自己的业务逻辑进行处理
        }
    }

返回结果示例

{
    "reason": "提交充值成功",
    "result": {
        "ordercash": "2.10",
        "cardname": "中国电信省内流量套餐10M",
        "sporder_id": "143212457209780362",
        "orderid": "a1122111ds1",
        "phone": "18913513535"
    },
    "error_code": 0
}

流量----订单状态查询:

请求参数

名称 必填 类型 说明
key String 在个人中心->我的数据,接口名称上方查看
orderid String 商家订单号,8-32位字母数字组合,请填写已经成功提交的订单号

代码示例

package com.tjrh.HttpRequest;

import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

public class tjrh_ll {
    private static OkHttpClient client;
   /**订单状态查询接口地址*/
    private static final String apiUrl_ordersta = "http://op.tianjurenhe.com/flow/ordersta";
    /**订单状态查询接口参数格式*/
    public static final String LL_ORDERSTA_PARAM = "?key={0}&orderid={1}";
    /**申请的接口Appkey*/
    public static final String key = 您申请的key;

    private static final int TIME_OUT = 20;

    static {
        client = new OkHttpClient.Builder()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }

    /**
     * 订单状态查询
     * @param orderId 商家订单号,8-32位字母数字组合,自定义
     * @return 返回String结果
     * @throws Exception
     */
    public static void ordersta(String orderId) throws IOException {
        //拼接地址与参数
        String url = apiUrl_ordersta + MessageFormat.format(LL_ORDERSTA_PARAM, key, orderId);
        //创建request对象实例
        Request request = new Request.Builder().url(url).build();
        //获取请求response应答
        Response response = client.newCall(request).execute();
        if(response != null){
            //返回消息体的字符串
            JSONObject result = JSONObject.parseObject(response.body().string());
            if (result.getInteger("error_code") == 0) {
                // 订单提交成功,根据实际业务逻辑进行处理
            }else{
                // 提交返回码error_code非正常状态,依据官方文档错误码说明,进行逻辑处理
                // 比如:10014,系统异常 / 208516,重复的订单号 等,需要进行二次查询/人工确认处理,不要直接失败处理,避免造成不必要的损失
            }
        }else{
            // 可能网络异常等问题,未获得正确响应结果,建议进行二次查单/人工确认,不要直接失败处理,避免造成不必要的损失
            // 依据自己的业务逻辑进行处理
        }
    }

返回结果示例

    "reason": "查询成功",
    "result": {
        "uordercash": "1.860",
        "sporder_id": "146944199833560022",
        "game_state": "9"/*0:充值中 1:成功 9:失败*/
    },
    "error_code": 0
}

评论区

励志做一条安静的咸鱼,从此走上人生巅峰。

0

0

0

举报