zoukankan      html  css  js  c++  java
  • Retrofit:后端发送HTTP请求

    引入Maven依赖:

    <dependency>
         <groupId>com.github.lianjiatech</groupId>
         <artifactId>retrofit-spring-boot-starter</artifactId>
         <version>2.2.5</version>
    </dependency>

    定义HTTP接口:

    import com.alibaba.fastjson.JSONObject;
    import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
    import retrofit2.http.*;
    import java.util.Map;
    
    @RetrofitClient(baseUrl = "https://api.doctorxiong.club")
    public interface httpApi {
    
        @GET("/v1/fund?code=202015,007339")
        Map<String,Object> httpGetTest();
    
        @POST("/v1/fund/rank")
        Map<String,Object> httpPostTest(@Body JSONObject jsonObject);
        
    }

    将HTTP接口注入Service中使用:

    @Service
    public class testServiceImpl {
        @Autowired
        private httpApi httpApi;
    
        @Scheduled(cron = "*/5 * * * * ?")
        public void TestTask() {
            JSONObject jsonObject = new JSONObject();
            List<Object> list = new ArrayList<>();
            list.add("zs");
            jsonObject.put("fundType",list);
            jsonObject.put("sort","z");
            List<Object> list1 = new ArrayList<>();
            list1.add("80000248");
            jsonObject.put("fundCompany",list1);
            jsonObject.put("pageIndex",1);
            jsonObject.put("pageSize",10);
            System.out.println(jsonObject);
            Map<String, Object> getMap = httpApi.httpGetTest();
            System.out.println(getMap);
            Map<String, Object> postMap = httpApi.httpPostTest(jsonObject);
            System.out.println(postMap);
        }
    
    }

    可使用的HTTP网址:https://www.doctorxiong.club/api/#api-Fund-fund

    Retrofit 的GitHub官网:https://github.com/LianjiaTech/retrofit-spring-boot-starter

    Retrofit 的官网文档:https://square.github.io/retrofit/#introduction

  • 相关阅读:
    iptables和DNS
    centos6.5下载
    linux 系统版本信息
    如何挂载
    Linux网络命令
    端口网络等
    linux安装tomcat
    ip设置
    最全DOS的CMD命令,程序员必会
    c语言文件分割与合并
  • 原文地址:https://www.cnblogs.com/zxg-6/p/14222701.html
Copyright © 2011-2022 走看看