多线程获取官方汇率

Posted Roam-G

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程获取官方汇率相关的知识,希望对你有一定的参考价值。

 

  • 接口地址:http://api.tianapi.com/fxrate/index 
  • 请求示例:http://api.tianapi.com/fxrate/index?key=APIKEY&fromcoin=USD&tocoin=CNY&money=1 
  • 支持协议:http/https
  • 请求方式:get/post
  • 返回格式:utf-8 json
  • post方式请求时,enctype应为application/x-www-form-urlencoded

    上传文件二进制数据流方式,enctype必须为multipart/form-data

    参数url、base64中有特殊字符时,建议对值urlencode编码后传递

    名称类型必填示例值/默认值说明
    keystring您自己的APIKEY(注册账号后获得)api密钥
    fromcoinstringUSD来源货币,例如美元USD
    tocoinstringCNY目标兑换货币,例如人民币CNY
    moneyint1

    兑换金额,单位元

错误代码错误消息解释帮助
100内部服务器错误报此错误码请及时反馈或等待官方修复
110当前API已下线接口已下线无法使用,可关注相关通知
120API暂时维护中接口暂时关闭维护中,请注意相关公告
130API调用频率超限超过每秒请求数上限,可在控制台-接口管理中查询
140API没有调用权限请检查是否自行在接口管理中停用或被禁用了该接口
150API可用次数不足账号余额不足以调用接口,请在控制台中查看
160账号未申请该API请先在接口文档页面申请该接口,点此查看说明
170Referer请求来源受限设置了Referer白名单,但来源Referer不在白名单内
180IP请求来源受限设置了IP白名单,但来源IP不在白名单内
190当前key已限制使用

通常为账号被限制使用,此状态无法恢复

如果您是post方式传值的,要么key参数应该在body消息体中,具体而言,每种编程语言都有细微差别,可在接口文档界面参考演示代码。

如果您确认已正确发送key参数给接口,但依然返回“缺少key参数”问题,则一般是数据编码格式问题。天行数据使用的默认编码格式。如果您的代码设置的是application/json等方式的编码格式,请添加或者修改为此种编码方式“application/x-www-form-urlencoded”。

对于java语言,如果使用的HttpCilentUtil工具类,请确保有如下类似的设置:

Response Header:
HTTP/1.1 200 OK
Date: Sat, 30 Jul 2022 09:29:59 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: *


Response Body:

  "code": 200,
  "msg": "success",
  "newslist": [
    
      "money": "6.7323"
    
  ]

//package com.mabang.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class MoneyRate implements Runnable

    /**
     * urlAll  请求接口
     * httpArg  参数
     * @return 返回结果
     */
//    int i = 0;
    public static String request(String httpUrl, String httpArg) 
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;
//        System.out.println(httpUrl);

        try 
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) 
                sbf.append(strRead);
                sbf.append("\\r\\n");
            
            reader.close();
            result = sbf.toString();
         catch (Exception e) 
            e.printStackTrace();
        
        return result;
    
    @Override
    public void run() 
        for(int i =0 ;i<100;i++ )

        if(Thread.currentThread().getName().equals("获取官方汇率"))
            try 
                Thread.sleep(1000);
//                单位是毫秒,1000毫秒 = 1秒。
             catch (Exception e) 
                System.out.println(e.getMessage());
                e.printStackTrace();
                //TODO: handle exception
            //end try


        //end if
        String httpUrl = "http://api.tianapi.com/fxrate/index";
        String httpArg = "key=7e2ffe4122153f108ae6a911f9829cdd&fromcoin=USD&tocoin=CNY&money=1";
        String jsonResult = request(httpUrl,httpArg);
        // system.out.println("1");
        System.out.println("第"+i+"次,获取官方汇率。------------------now time :"+new Date());
        System.out.println(jsonResult);

        
    


    public static void main(String[] args) 
        //java环境中文传值时,需特别注意字符编码问题

        MoneyRate rate = new MoneyRate();
        new Thread(rate,"获取官方汇率").start();
    



第0次,获取官方汇率。------------------now time :Sat Jul 30 17:57:30 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第1次,获取官方汇率。------------------now time :Sat Jul 30 17:57:31 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第2次,获取官方汇率。------------------now time :Sat Jul 30 17:57:33 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第3次,获取官方汇率。------------------now time :Sat Jul 30 17:57:34 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第4次,获取官方汇率。------------------now time :Sat Jul 30 17:57:35 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第5次,获取官方汇率。------------------now time :Sat Jul 30 17:57:36 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第6次,获取官方汇率。------------------now time :Sat Jul 30 17:57:37 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第7次,获取官方汇率。------------------now time :Sat Jul 30 17:57:38 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第8次,获取官方汇率。------------------now time :Sat Jul 30 17:57:39 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第9次,获取官方汇率。------------------now time :Sat Jul 30 17:57:40 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第10次,获取官方汇率。------------------now time :Sat Jul 30 17:57:41 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第11次,获取官方汇率。------------------now time :Sat Jul 30 17:57:42 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第12次,获取官方汇率。------------------now time :Sat Jul 30 17:57:43 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第13次,获取官方汇率。------------------now time :Sat Jul 30 17:57:44 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第14次,获取官方汇率。------------------now time :Sat Jul 30 17:57:45 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第15次,获取官方汇率。------------------now time :Sat Jul 30 17:57:46 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第16次,获取官方汇率。------------------now time :Sat Jul 30 17:57:47 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第17次,获取官方汇率。------------------now time :Sat Jul 30 17:57:48 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第18次,获取官方汇率。------------------now time :Sat Jul 30 17:57:49 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第19次,获取官方汇率。------------------now time :Sat Jul 30 17:57:50 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第20次,获取官方汇率。------------------now time :Sat Jul 30 17:57:51 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第21次,获取官方汇率。------------------now time :Sat Jul 30 17:57:52 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第22次,获取官方汇率。------------------now time :Sat Jul 30 17:57:53 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第23次,获取官方汇率。------------------now time :Sat Jul 30 17:57:54 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第24次,获取官方汇率。------------------now time :Sat Jul 30 17:57:55 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第25次,获取官方汇率。------------------now time :Sat Jul 30 17:57:56 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第26次,获取官方汇率。------------------now time :Sat Jul 30 17:57:57 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第27次,获取官方汇率。------------------now time :Sat Jul 30 17:57:58 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第28次,获取官方汇率。------------------now time :Sat Jul 30 17:57:59 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第29次,获取官方汇率。------------------now time :Sat Jul 30 17:58:01 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第30次,获取官方汇率。------------------now time :Sat Jul 30 17:58:02 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第31次,获取官方汇率。------------------now time :Sat Jul 30 17:58:03 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第32次,获取官方汇率。------------------now time :Sat Jul 30 17:58:04 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第33次,获取官方汇率。------------------now time :Sat Jul 30 17:58:05 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第34次,获取官方汇率。------------------now time :Sat Jul 30 17:58:06 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第35次,获取官方汇率。------------------now time :Sat Jul 30 17:58:07 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第36次,获取官方汇率。------------------now time :Sat Jul 30 17:58:08 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第37次,获取官方汇率。------------------now time :Sat Jul 30 17:58:09 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第38次,获取官方汇率。------------------now time :Sat Jul 30 17:58:10 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第39次,获取官方汇率。------------------now time :Sat Jul 30 17:58:11 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第40次,获取官方汇率。------------------now time :Sat Jul 30 17:58:12 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第41次,获取官方汇率。------------------now time :Sat Jul 30 17:58:13 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第42次,获取官方汇率。------------------now time :Sat Jul 30 17:58:14 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第43次,获取官方汇率。------------------now time :Sat Jul 30 17:58:15 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第44次,获取官方汇率。------------------now time :Sat Jul 30 17:58:16 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第45次,获取官方汇率。------------------now time :Sat Jul 30 17:58:17 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第46次,获取官方汇率。------------------now time :Sat Jul 30 17:58:18 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第47次,获取官方汇率。------------------now time :Sat Jul 30 17:58:19 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第48次,获取官方汇率。------------------now time :Sat Jul 30 17:58:20 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第49次,获取官方汇率。------------------now time :Sat Jul 30 17:58:21 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第50次,获取官方汇率。------------------now time :Sat Jul 30 17:58:22 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第51次,获取官方汇率。------------------now time :Sat Jul 30 17:58:24 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第52次,获取官方汇率。------------------now time :Sat Jul 30 17:58:25 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第53次,获取官方汇率。------------------now time :Sat Jul 30 17:58:26 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第54次,获取官方汇率。------------------now time :Sat Jul 30 17:58:27 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第55次,获取官方汇率。------------------now time :Sat Jul 30 17:58:28 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第56次,获取官方汇率。------------------now time :Sat Jul 30 17:58:29 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第57次,获取官方汇率。------------------now time :Sat Jul 30 17:58:30 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第58次,获取官方汇率。------------------now time :Sat Jul 30 17:58:31 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第59次,获取官方汇率。------------------now time :Sat Jul 30 17:58:32 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第60次,获取官方汇率。------------------now time :Sat Jul 30 17:58:33 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第61次,获取官方汇率。------------------now time :Sat Jul 30 17:58:34 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第62次,获取官方汇率。------------------now time :Sat Jul 30 17:58:35 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第63次,获取官方汇率。------------------now time :Sat Jul 30 17:58:36 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第64次,获取官方汇率。------------------now time :Sat Jul 30 17:58:37 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第65次,获取官方汇率。------------------now time :Sat Jul 30 17:58:38 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第66次,获取官方汇率。------------------now time :Sat Jul 30 17:58:39 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第67次,获取官方汇率。------------------now time :Sat Jul 30 17:58:40 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第68次,获取官方汇率。------------------now time :Sat Jul 30 17:58:41 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第69次,获取官方汇率。------------------now time :Sat Jul 30 17:58:42 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第70次,获取官方汇率。------------------now time :Sat Jul 30 17:58:43 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第71次,获取官方汇率。------------------now time :Sat Jul 30 17:58:44 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第72次,获取官方汇率。------------------now time :Sat Jul 30 17:58:45 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第73次,获取官方汇率。------------------now time :Sat Jul 30 17:58:46 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第74次,获取官方汇率。------------------now time :Sat Jul 30 17:58:48 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第75次,获取官方汇率。------------------now time :Sat Jul 30 17:58:49 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第76次,获取官方汇率。------------------now time :Sat Jul 30 17:58:50 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第77次,获取官方汇率。------------------now time :Sat Jul 30 17:58:51 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第78次,获取官方汇率。------------------now time :Sat Jul 30 17:58:52 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第79次,获取官方汇率。------------------now time :Sat Jul 30 17:58:53 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第80次,获取官方汇率。------------------now time :Sat Jul 30 17:58:54 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第81次,获取官方汇率。------------------now time :Sat Jul 30 17:58:55 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第82次,获取官方汇率。------------------now time :Sat Jul 30 17:58:56 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第83次,获取官方汇率。------------------now time :Sat Jul 30 17:58:57 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第84次,获取官方汇率。------------------now time :Sat Jul 30 17:58:58 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第85次,获取官方汇率。------------------now time :Sat Jul 30 17:58:59 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第86次,获取官方汇率。------------------now time :Sat Jul 30 17:59:00 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第87次,获取官方汇率。------------------now time :Sat Jul 30 17:59:01 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第88次,获取官方汇率。------------------now time :Sat Jul 30 17:59:02 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第89次,获取官方汇率。------------------now time :Sat Jul 30 17:59:03 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第90次,获取官方汇率。------------------now time :Sat Jul 30 17:59:04 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第91次,获取官方汇率。------------------now time :Sat Jul 30 17:59:05 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第92次,获取官方汇率。------------------now time :Sat Jul 30 17:59:06 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第93次,获取官方汇率。------------------now time :Sat Jul 30 17:59:07 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第94次,获取官方汇率。------------------now time :Sat Jul 30 17:59:08 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第95次,获取官方汇率。------------------now time :Sat Jul 30 17:59:10 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第96次,获取官方汇率。------------------now time :Sat Jul 30 17:59:11 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第97次,获取官方汇率。------------------now time :Sat Jul 30 17:59:12 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第98次,获取官方汇率。------------------now time :Sat Jul 30 17:59:13 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

第99次,获取官方汇率。------------------now time :Sat Jul 30 17:59:14 CST 2022
"code":200,"msg":"success","newslist":["money":"6.7323"]

以上是关于多线程获取官方汇率的主要内容,如果未能解决你的问题,请参考以下文章

汇率多线程 草稿

线程安全

JAVA多线程用实现Runnable接口的方式创建线程

java多线程-信号量

Java-多线程实验

java多线程回顾3:线程安全